package com.moody.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.calculator.EnterCheck;
import com.moody.calculator.FinanFinancialIndexCalculator;
import com.moody.calculator.FinanVerify;
import com.moody.calculator.FinancialIndexCalculator;
import com.moody.calculator.PublicFinancialIndexCalculator;
import com.moody.calculator.PublicVerify;
import com.moody.calculator.SmallCheck;
import com.moody.calculator.SmallFinancialIndexCalculator;
import com.moody.constant.DataDictionaryConstant;
import com.moody.hibernate.dao.BankCustDepositDAO;
import com.moody.hibernate.dao.BankCustGuaranteeDAO;
import com.moody.hibernate.dao.BankCustLoanDAO;
import com.moody.hibernate.dao.CustAddressDAO;
import com.moody.hibernate.dao.CustEventsDAO;
import com.moody.hibernate.dao.CustInfoDAO;
import com.moody.hibernate.dao.CustManagerDAO;
import com.moody.hibernate.dao.IndInfoDAO;
import com.moody.hibernate.dao.ReportListDAO;
import com.moody.hibernate.domain.AssociateAccount;
import com.moody.hibernate.domain.BankCustDeposit;
import com.moody.hibernate.domain.BankCustGuarantee;
import com.moody.hibernate.domain.BankCustLoan;
import com.moody.hibernate.domain.CustAddress;
import com.moody.hibernate.domain.CustEvents;
import com.moody.hibernate.domain.CustInfo;
import com.moody.hibernate.domain.CustManager;
import com.moody.hibernate.domain.CustStockholder;
import com.moody.hibernate.domain.DebtInfo;
import com.moody.hibernate.domain.EnterBalanceSheet;
import com.moody.hibernate.domain.EnterCashFlow;
import com.moody.hibernate.domain.EnterFinancialIndex;
import com.moody.hibernate.domain.EnterIncome;
import com.moody.hibernate.domain.ExtratInfo;
import com.moody.hibernate.domain.FinanBalanceSheet;
import com.moody.hibernate.domain.FinanFinancialIndexInputSheet;
import com.moody.hibernate.domain.FinanFinancialIndexSheet;
import com.moody.hibernate.domain.FinanIncomeSheet;
import com.moody.hibernate.domain.GroupInfo;
import com.moody.hibernate.domain.IndBank;
import com.moody.hibernate.domain.IndInfo;
import com.moody.hibernate.domain.IndOccard;
import com.moody.hibernate.domain.IndOguarantee;
import com.moody.hibernate.domain.IndOloan;
import com.moody.hibernate.domain.MemberInfo;
import com.moody.hibernate.domain.ModelHis;
import com.moody.hibernate.domain.OrgInfo;
import com.moody.hibernate.domain.PdH;
import com.moody.hibernate.domain.PublicBalanceSheet;
import com.moody.hibernate.domain.PublicFinancialIndex;
import com.moody.hibernate.domain.PublicIncomeSheet;
import com.moody.hibernate.domain.ReportData;
import com.moody.hibernate.domain.ReportDataId;
import com.moody.hibernate.domain.ReportList;
import com.moody.hibernate.domain.ReportListId;
import com.moody.hibernate.domain.ReportModel;
import com.moody.hibernate.domain.SheetNum;
import com.moody.hibernate.domain.SmallBalanceSheet;
import com.moody.hibernate.domain.SmallCashFlowSheet;
import com.moody.hibernate.domain.SmallFinancialIndexSheet;
import com.moody.hibernate.domain.SmallIncomeSheet;
import com.moody.hibernate.domain.SpecialRecord;
import com.moody.hibernate.domain.SpecialTrade;
import com.moody.hibernate.domain.UserInfo;
import com.moody.init.DataDictionary;
import com.moody.init.Industry;
import com.moody.init.ReportItemInit;
import com.moody.init.ReportModelInit;
import com.moody.service.CustomerService;
import com.moody.service.DispatchService;
import com.moody.service.ModelService;
import com.moody.service.SystemService;
import com.moody.util.CalendarUtil;

/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-8-30 上午09:43:35
 * @Modified by: Yan Jian
 * @Description: 
 */

@Controller
@RequestMapping("/customer")
public class CustomerController {
	private static final Logger log = LoggerFactory
			.getLogger(CustomerController.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private CustAddressDAO custAddressDao = CustAddressDAO
			.getFromApplicationContext(ctx);
	private CustInfoDAO custInfoDao = CustInfoDAO
			.getFromApplicationContext(ctx);
	private CustManagerDAO custManagerDao = CustManagerDAO
			.getFromApplicationContext(ctx);
	private CustEventsDAO custEventsDao = CustEventsDAO
			.getFromApplicationContext(ctx);
	private BankCustDepositDAO bankCustDepositDao = BankCustDepositDAO
			.getFromApplicationContext(ctx);
	private BankCustLoanDAO bankCustLoanDao = BankCustLoanDAO
			.getFromApplicationContext(ctx);
	private BankCustGuaranteeDAO bankCustGuaranteeDAO = BankCustGuaranteeDAO
			.getFromApplicationContext(ctx);
	private IndInfoDAO indInfoDAO = IndInfoDAO
			.getFromApplicationContext(ctx);

	@Resource
	private CustomerService customerService;
	@Resource
	private DispatchService dispatchService;
	@Resource
	private ModelService modelService;

	/*
	 * 获取客户列表
	 */
	@RequestMapping("/listCustomer")
	public void listCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		String custType = request.getParameter("CustType");
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();

		List<CustInfo> myCustList = new ArrayList<CustInfo>();
		if(custType!=null && !custType.equals("") ){
			myCustList = customerService.findAllByCustType(custType,userId,orgId);
		}else{
			myCustList = customerService.findAllByCustType("",userId,orgId);
		}
		// myCustList.subList(fromIndex, toIndex)
		// 如果custType为空，查找所有属于该用户的custList
		// TODO
		List<CustInfo> pageCustList;
//		String userId = "";
//		if (custType == null || custType.equals("")) {
//			myCustList = customerService.findAllCustomerByUserId(userId);
//		}

		// 分页,该页size未超过剩余size
		if (myCustList.size() > start + limit)
			pageCustList = myCustList.subList(start, limit + start);
		else
			pageCustList = myCustList.subList(start, myCustList.size());

		for (CustInfo custInfo : pageCustList) {
			if (custInfo.getIndustryType() != null && !custInfo.getIndustryType().equals("")) {
				custInfo.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType",custInfo.getIndustryType()));
			}
			if (custInfo.getDefaultStat() != null && !custInfo.getDefaultStat().equals("")) {
				custInfo.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat",custInfo.getDefaultStat()));
			}
		}

		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("total", myCustList.size());
		myCustMap.put("customer", pageCustList);
		JSONObject myCustJO = JSONObject.fromObject(myCustMap);
		out.print(myCustJO);
	}


	//查询客户
	@RequestMapping("/findCustomer")
	public void findCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String custType = request.getParameter("CustType");
		String conditionKey = request.getParameter("condition");
		String conditionValue = request.getParameter("keyword");
		List<CustInfo> list = new ArrayList<CustInfo>(); 
		Map<String,String> condition = new LinkedHashMap<String,String>();
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		
		if(custType == null) custType = "";
		if(conditionKey.equals("custId")) conditionKey = "custNo";//转换成按照客户号查询
		condition.put("custType", custType);
		condition.put(conditionKey, conditionValue);
		list = customerService.findCustByCondition(condition, userId, orgId);
//		custList = customerService.findCustomerByIdAndName(conditionKey, conditionValue);
//		List<CustInfo> list = new ArrayList<CustInfo>();
//		for(CustInfo custInfo : custList){
//			if(custType!=null && !custInfo.getCustType().equals(custType)){
//				//custList.remove(custInfo);
//			}else{
//				list.add(custInfo);
//			}
//		}
		
		//分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		
		List<CustInfo> pageList;
		if(list.size()>start+limit)
			pageList = list.subList(start, limit+start); 
		else
			pageList = list.subList(start,list.size());
		
		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("success", "true");
		myCustMap.put("total", list.size());
		myCustMap.put("resultData", pageList);
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
		
		log.info("JSON data: "+myCustJO);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(myCustJO);
		
	}
	
	
	/*
	 * 查看客户详情
	 */
	@RequestMapping("/checkMyCustomer")
	public String checkMyCustomer(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		request.getSession().removeAttribute("custId");

		String custId = request.getParameter("CustId");
		CustInfo selectedCust = new CustInfo();
		selectedCust = customerService.findCustByCustId(custId);
		model.addAttribute("CustInfo", selectedCust);
		request.getSession().removeAttribute("custId");
		request.getSession().setAttribute("custId", custId);
		request.getSession().removeAttribute("custInfo");
		request.getSession().setAttribute("custInfo", selectedCust);
		request.getSession().removeAttribute("custName");
		request.getSession().setAttribute("custName",
				selectedCust.getCustName());
		request.getSession().removeAttribute("custType");
		request.getSession().setAttribute(
				"custType",
				DataDictionary.getInstance().getNameByCodeAndId("CustType",
						selectedCust.getCustType()));

		return "customer/Customer_General";
	}

	/*
	 * 检查当前客户是否存在
	 */
	@RequestMapping("/checkCustomer")
	public void checkCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custName = request.getParameter("CustName");
		String custNo = request.getParameter("CustNo");
		String organizationID = request.getParameter("OrganizationID");
		String busiLicenseID = request.getParameter("BusiLicenseID");
		int isExit = 0;

		//custNo 不做唯一性判断（见底层sql语句）
		if (customerService.isCustExist(custName, custNo,organizationID, busiLicenseID)) {
			isExit = 1;
			response.getWriter().print(isExit);
		} else {
			isExit = 0;
			response.getWriter().print(isExit);
		}

	}

	/*
	 * 添加客户 添加客户的最基本信息
	 */
	@RequestMapping("/addMain")
	public String addMain(HttpServletRequest request,
			HttpServletResponse response, CustInfo custInfo, Model model)
			throws IOException {
		if (request.getParameter("CustType") != null
				&& !request.getParameter("CustType").equals(""))
			custInfo.setCustType(request.getParameter("CustType"));

		// 设置当前时间
		custInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		
		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		
		custInfo.setInputUserId(userId);
		custInfo.setInputOrgId(orgId);

		// 设置为空，不然搜索功能不到
		custInfo.setCreditLevel("");
		custInfo.setIndustryType("");
		custInfo.setDefaultStat("0");

		if(!customerService.isCustExist(custInfo.getCustName(), custInfo.getCustNo(),"", ""))customerService.insertGeneral(custInfo);
		else custInfo = customerService.findCustByCustNo(custInfo.getCustNo());
		model.addAttribute("CustInfo", custInfo);

		request.getSession().removeAttribute("custId");
		request.getSession().setAttribute("custId", custInfo.getCustId());

		request.getSession().removeAttribute("custName");
		request.getSession().setAttribute("custName", custInfo.getCustName());
		
		request.getSession().removeAttribute("custInfo");
		request.getSession().setAttribute("custInfo", custInfo);

		request.getSession().removeAttribute("custType");
		request.getSession().setAttribute(
				"custType",
				DataDictionary.getInstance().getNameByCodeAndId("CustType",
						custInfo.getCustType()));

		log.info("DataDictionary: " + DataDictionary.getInstance());
		// response.getWriter().print("{success:true}");

		return "customer/Customer_General";
	}

	/*
	 * 添加客户 添加保证人的最基本信息
	 */
	@RequestMapping("/addGuarantor")
	public void addGuarantor(HttpServletRequest request,
			HttpServletResponse response, CustInfo custInfo, Model model)
			throws IOException {
		if (request.getParameter("custType") != null
				&& !request.getParameter("custType").equals(""))
			custInfo.setCustType(request.getParameter("custType"));

		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		// 设置当前时间
		custInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		custInfo.setInputUserId(userId);
		custInfo.setInputOrgId(orgId);
		custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());

		// 保证人信息写入db
		customerService.insertGeneral(custInfo);
//		model.addAttribute("CustInfo", custInfo);
//		request.getSession().removeAttribute("custId");
//		request.getSession().setAttribute("custId", custInfo.getCustId());
//		
//		request.getSession().removeAttribute("custInfo");
//		request.getSession().setAttribute("custInfo", custInfo);

		// 外部评级信息写入db
		ExtratInfo extratInfo = new ExtratInfo();
		extratInfo.setCustId(custInfo.getCustId());
		extratInfo.setExtRatDate(request.getParameter("extRatDate"));
		extratInfo.setExtRatOrg(request.getParameter("extRatOrg"));
		extratInfo.setExtRatResult(request.getParameter("extRatResult"));
		extratInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		extratInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.insertExtrat(extratInfo);

		log.info("DataDictionary: " + DataDictionary.getInstance());
		
		
		Map<String, Object> myCustMap = new HashMap<String, Object>(2);
		myCustMap.put("success", "true");
		myCustMap.put("custId", custInfo.getCustId());
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
		
		log.info("JSON data: "+myCustJO);
//		response.setContentType("application/json; charset=utf-8");  
//		response.setHeader("Cache-Control", "no-cache");
//		response.getWriter().print(custInfo.getCustId());

	}

	/*
	 * 客户概况信息操作
	 */

	// 添加客户信息 基本信息
	@RequestMapping("/addGeneral")
	public String addGeneral(HttpServletRequest request,
			HttpServletResponse response, CustInfo custInfo, String isView,
			@RequestParam(value = "custId", required = false) String custId,
			Model model) {

		if (isView != null && !isView.equals(""))
			custInfo = customerService.findCustByCustId(custId);
		else
			customerService.updateCustInfo(custInfo);
		model.addAttribute("CustInfo", custInfo);

		return "customer/AddCustomer_General";
	}

	@RequestMapping("/getGeneral")
	public void getGeneral(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId;
		CustInfo custInfo = new CustInfo();
		
		if(request.getParameter("isGuarantor") != null){
			custId = request.getParameter("custId");
			custInfo = customerService.findCustAndCreditByCustId(custId);
		}
		else{
			custId = request.getSession().getAttribute("custId").toString();
			custInfo = customerService.findCustByCustId(custId);
		}
		
		
		
		Map<String, String> industry = Industry.getInstance().getIndustry();
		if(custInfo.getIndustryType()!=null && !custInfo.getIndustryType().equals("")){
			custInfo.setIndustryTypeLevelOne(industry.get(custInfo
				.getIndustryType().substring(0, 1)));
			custInfo.setIndustryTypeLevelTwo(industry.get(custInfo
				.getIndustryType()));
		}
		
		Map map = new HashMap();
		map.put("success", true);
		map.put("data", custInfo);

		JSONObject custJO;
		// 如果是从添加保证人页面进入
		if (request.getParameter("isGuarantor") != null) {
			custJO = JSONObject.fromObject(custInfo);
		} else {
			custJO = JSONObject.fromObject(map);
		}
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(custJO);
	}

	// 编辑基本信息和编辑经营情况都复用此方法
	@RequestMapping("/editGeneral")
	public void editGeneral(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("custId").toString();
		CustInfo custInfo = new CustInfo();
		custInfo = customerService.findCustByCustId(custId);
		String isOperation = request.getParameter("isOperation");

		if (custInfo != null && !("1").equals(isOperation)) {
			custInfo.setCustNo(request.getParameter("custNo"));
			custInfo.setSimpleName(request.getParameter("simpleName"));
			custInfo.setEnglishName(request.getParameter("englishName"));
			custInfo.setSimpleEnName(request.getParameter("simpleEnName"));
			custInfo.setBelongType(request.getParameter("belongType"));
			custInfo.setOrgType(request.getParameter("orgType"));
			custInfo.setEstablishDate(request.getParameter("establishDate"));
			custInfo.setIndustryType(getIndustryType(request
					.getParameter("industryTypeLevelTwo")));
			custInfo.setCloseDate(request.getParameter("closeDate"));
			custInfo.setRegistercapital(Double.valueOf(request
					.getParameter("registercapital")));
			custInfo.setCapitalCurrency(request.getParameter("capitalCurrency"));
			custInfo.setReceiveRate(Double.valueOf(request
					.getParameter("receiveRate")));
			custInfo.setCustStatus(request.getParameter("custStatus"));
			custInfo.setCustType(request.getParameter("custType"));
			custInfo.setCustScope(request.getParameter("custScope"));
			
			custInfo.setDebtCardNo(request.getParameter("debtCardNo"));
			custInfo.setDebtCardPwd(request.getParameter("debtCardPwd"));
			custInfo.setRegisterDate(request.getParameter("registerDate"));
			custInfo.setRelationshipDate(request.getParameter("relationshipDate"));
			custInfo.setTwoHighFlag(request.getParameter("twoHighFlag"));
			custInfo.setLimitedFlag(request.getParameter("limitedFlag"));
			custInfo.setConfirmedSmallFlag(request.getParameter("confirmedSmallFlag"));
			custInfo.setPublicFlag(request.getParameter("publicFlag"));
			custInfo.setNationalTaxNo(request.getParameter("nationalTaxNo"));
			custInfo.setLocalTaxNo(request.getParameter("localTaxNo"));
			custInfo.setDebtPercent(Double.valueOf(request.getParameter("debtPercent")));
			custInfo.setInOutFlag(request.getParameter("inOutFlag"));
			custInfo.setGroupFlag(request.getParameter("groupFlag"));
			
			custInfo.setCountryCode(request.getParameter("countryCode"));
			custInfo.setProvinceCode(request.getParameter("provinceCode"));
			custInfo.setCityCode(request.getParameter("cityCode"));
			
			custInfo.setPeopleNum((int)Math.floor(Double.valueOf(request.getParameter("peopleNum"))));

			customerService.updateCustInfo(custInfo);
		} else {
			custInfo.setBusiScope(request.getParameter("busiScope"));
			custInfo.setSidelineScope(request.getParameter("sidelineScope"));
			custInfo.setRelatedTransaction(request.getParameter("relatedTransaction"));
			if (request.getParameter("last3avgincome") != null)
				custInfo.setLast3avgincome(Double.valueOf(request.getParameter(
						"last3avgincome").toString()));
			if (request.getParameter("last2income") != null)
				custInfo.setLast2income(Double.valueOf(request.getParameter(
						"last2income").toString()));
			if (request.getParameter("lastIncome") != null)
				custInfo.setLastIncome(Double.valueOf(request.getParameter(
						"lastIncome").toString()));
			if (request.getParameter("assetSum") != null)
				custInfo.setAssetSum(Double.valueOf(request.getParameter(
						"assetSum").toString()));
			if (request.getParameter("debtSum") != null)
				custInfo.setDebtSum(Double.valueOf(request.getParameter(
						"debtSum").toString()));
			if (request.getParameter("licenceBusiScope") != null)
				custInfo.setLicenceBusiScope(request.getParameter(
						"licenceBusiScope").toString());
			if (request.getParameter("operationYears") != null)
				custInfo.setOperationYears(request.getParameter(
						"operationYears").toString());
			if (request.getParameter("mainBusiIncome") != null)
				custInfo.setMainBusiIncome(Double.valueOf(request.getParameter(
						"mainBusiIncome").toString()));
			if (request.getParameter("ownersEquity") != null)
				custInfo.setOwnersEquity(Double.valueOf(request.getParameter(
						"ownersEquity").toString()));
			
			
			custInfo.setOtherDesc(request.getParameter("otherDesc"));

			customerService.updateCustInfo(custInfo);
		}
	}

	private String getIndustryType(String levelTwo) {
		if (levelTwo == null || levelTwo.equals(""))
			return null;
		StringTokenizer st = new StringTokenizer(levelTwo);
		return st.nextToken();
	}

	/*
	 * 客户地址信息操作
	 */

	// 获取地址列表
	@RequestMapping("/listAddress")
	public void listAddress(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<CustAddress> list = new ArrayList<CustAddress>();

		if (custId != null && !custId.equals(""))
			list = customerService.findAllByCustId(custId);

		// 分页列表list
		List<CustAddress> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (CustAddress ca : pageList) {
			ca.setAddressType(DataDictionary.getInstance().getNameByCodeAndId(
					"AddressType", ca.getAddressType()));
			ca.setCityCode(DataDictionary.getInstance().getNameByCodeAndId(
					DataDictionaryConstant.REGIONCODE, ca.getCityCode()));
			ca.setCountryCode(DataDictionary.getInstance().getNameByCodeAndId(
					"CountryCode", ca.getCountryCode()));
			ca.setProvinceCode(DataDictionary.getInstance().getNameByCodeAndId(
					DataDictionaryConstant.REGIONCODE, ca.getProvinceCode()));
		}


		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("total", list.size());
		myCustMap.put("address", pageList);
		JSONObject myCustJO = JSONObject.fromObject(myCustMap);
		out.print(myCustJO);

	}

	/*
	 * 客户外部评级信息操作
	 */

	// 获取外部评级列表
	@RequestMapping("/listExtrat")
	public void listExtrat(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<ExtratInfo> list = new ArrayList<ExtratInfo>();

		if (custId != null && !custId.equals(""))
			list = customerService.findExtratByCustId(custId);

		for (ExtratInfo ca : list) {
			ca.setExtRatOrg(DataDictionary.getInstance().getNameByCodeAndId(
					"ExtRatOrg", ca.getExtRatOrg()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 外部评级信息
	@RequestMapping("/addExtrat")
	public void addExtrat(HttpServletRequest request,
			HttpServletResponse response, ExtratInfo extratInfo, Model model)
			throws IOException {
		log.info(("ExtRatResult: " + extratInfo.getExtRatResult()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			extratInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());

		customerService.insertExtrat(extratInfo);

		response.getWriter().print("{success:true}");
	}

	// 删除外部评级信息
	@RequestMapping("/deleteExtrat")
	public void deleteExtrat(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteExtratById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 获取单一外部评级
	@RequestMapping("/getExtrat")
	public void getExtrat(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		ExtratInfo extratInfo = new ExtratInfo();
		extratInfo = customerService.findExtratById(addSerialNo);
		JSONObject extratJO = JSONObject.fromObject(extratInfo);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(extratJO);
	}

	// 编辑客户外部评级
	@RequestMapping("/editExtrat")
	public void editExtrat(HttpServletRequest request,
			HttpServletResponse response, ExtratInfo extratInfo)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			extratInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateExtrat(extratInfo);
		}

		response.getWriter().print("{success:true}");
	}

	// 添加客户信息 地址信息
	@RequestMapping("/addAddress")
	public void addAddress(HttpServletRequest request,
			HttpServletResponse response, CustAddress custAddress, Model model)
			throws IOException {
		log.info(("AddressType: " + custAddress.getAddressType()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			custAddress.setCustId(request.getSession().getAttribute("custId")
					.toString());

		customerService.insertAddress(custAddress);

		response.getWriter().print("{success:true}");
	}

	// 获取单一地址信息
	@RequestMapping("/getAddress")
	public void getAddress(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		CustAddress custAddress = new CustAddress();
		custAddress = customerService.findAddressById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(custAddress);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	// 编辑客户地址信息
	@RequestMapping("/editAddress")
	public void editAddress(HttpServletRequest request,
			HttpServletResponse response, CustAddress custAddress)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			custAddress.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateAddress(custAddress);
		}

		response.getWriter().print("{success:true}");
	}

	// 删除客户地址信息
	@RequestMapping("/deleteAddress")
	public void deleteAddress(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteAddressById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	/*
	 * 客户经营情况操作
	 */

	// 添加客户信息 经营情况
	@RequestMapping("/addOperation")
	public String addOperation(HttpServletRequest request,
			HttpServletResponse response, CustInfo custInfo, Model model) {

		Object last3avgincome = request.getParameter("last3avgincome");
		Object lastIncome = request.getParameter("lastIncome");
		Object last2Income = request.getParameter("last2income");
		Object assetSum = request.getParameter("assetSum");
		Object debtSum = request.getParameter("debtSum");
		Object otherDesc = request.getParameter("otherDesc");
		Object busiScope = request.getParameter("busiScope");
		Object sidelineScope = request.getParameter("sidelineScope");

		String custId = request.getSession().getAttribute("custId").toString();

		custInfo = custInfoDao.findById(custId);
		if (busiScope != null && !busiScope.toString().equals(""))
			custInfo.setBusiScope(busiScope.toString());
		if (sidelineScope != null && !sidelineScope.toString().equals(""))
			custInfo.setSidelineScope(sidelineScope.toString());
		if (last3avgincome != null && !last3avgincome.toString().equals(""))
			custInfo.setLast3avgincome(Double.parseDouble(last3avgincome
					.toString()));
		if (lastIncome != null && !lastIncome.toString().equals(""))
			custInfo.setLastIncome(Double.parseDouble(lastIncome.toString()));
		if (last2Income != null && !last2Income.toString().equals(""))
			custInfo.setLast2income(Double.parseDouble(last2Income.toString()));
		if (assetSum != null && !assetSum.toString().equals(""))
			custInfo.setAssetSum(Double.parseDouble(assetSum.toString()));
		if (debtSum != null && !debtSum.toString().equals(""))
			custInfo.setDebtSum(Double.parseDouble(debtSum.toString()));
		if (otherDesc != null && !otherDesc.toString().equals(""))
			custInfo.setOtherDesc(otherDesc.toString());

		if (custInfo != null)
			customerService.updateCustInfo(custInfo);
		model.addAttribute("CustInfo", custInfo);

		return "customer/AddCustomer_Operation";
	}

	/*
	 * 客户 股东信息操作
	 */

	// 获取股东列表
	@RequestMapping("/listStockholder")
	public void listStockholder(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<CustStockholder> list = new ArrayList<CustStockholder>();

		if (custId != null && !custId.equals(""))
			list = customerService.findStockholderByCustId(custId);
		
		for (CustStockholder cm : list) {
			cm.setCurrency(DataDictionary.getInstance().getNameByCodeAndId(
					"Currency", cm.getCurrency()));
			cm.setHoldType(DataDictionary.getInstance().getNameByCodeAndId(
					"HoldType", cm.getHoldType()));
			cm.setStockholderType(DataDictionary.getInstance().getNameByCodeAndId(
					"StockholderType", cm.getStockholderType()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 股东信息
	@RequestMapping("/addStockholder")
	public void addStockholder(HttpServletRequest request,
			HttpServletResponse response, CustStockholder custStockholder, Model model)
			throws IOException {
		log.info(("AddressType: " + custStockholder.getStockholderName()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			custStockholder.setCustId(request.getSession().getAttribute("custId")
					.toString());
		custStockholder.setInputDate(CalendarUtil.getCurrentDateStr());
		custStockholder.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.insertCustStockholder(custStockholder);

		response.getWriter().print("{success:true}");
	}

	// 获取单一股东信息信息
	@RequestMapping("/getStockholder")
	public void getStockholder(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		CustStockholder custStockholder = new CustStockholder();
		custStockholder = customerService.findStockholderById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(custStockholder);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	// 编辑客户股东信息信息
	@RequestMapping("/editStockholder")
	public void editAddress(HttpServletRequest request,
			HttpServletResponse response, CustStockholder custStockholder)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			custStockholder.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateStockholder(custStockholder);
		}

		response.getWriter().print("{success:true}");
	}

	// 删除客户股东信息
	@RequestMapping("/deleteStockholder")
	public void deleteStockholder(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteStockholderById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	/*
	 * 客户 高管信息操作
	 */

	// 获取高管列表
	@RequestMapping("/listManager")
	public void listManager(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<CustManager> list = new ArrayList<CustManager>();

		if (custId != null && !custId.equals(""))
			list = customerService.findManagerByCustId(custId);

		for (CustManager cm : list) {
			cm.setEducation(DataDictionary.getInstance().getNameByCodeAndId(
					"Education", cm.getEducation()));
			cm.setSexType(DataDictionary.getInstance().getNameByCodeAndId(
					"SexType", cm.getSexType()));
			cm.setJobtitle(DataDictionary.getInstance().getNameByCodeAndId(
					"Position", cm.getJobtitle()));
			cm.setNationality(DataDictionary.getInstance().getNameByCodeAndId(
					"CountryCode", cm.getNationality()));
			cm.setIdtype(DataDictionary.getInstance().getNameByCodeAndId(
					"CertType", cm.getIdtype()));
			cm.setIsPrivateCust(DataDictionary.getInstance().getNameByCodeAndId("YesNo", cm.getIsPrivateCust()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 管理层
	@RequestMapping("/addManager")
	public void addManager(HttpServletRequest request,
			HttpServletResponse response, CustManager custManager, Model model)
			throws IOException {
		log.info(("AddressType: " + custManager.getManagerName()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			custManager.setCustId(request.getSession().getAttribute("custId")
					.toString());
		custManager.setInputDate(CalendarUtil.getCurrentDateStr());
		custManager.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.insertCustManager(custManager);

		response.getWriter().print("{success:true}");
	}

	// 获取单一管理层信息
	@RequestMapping("/getManager")
	public void getManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		CustManager custManager = new CustManager();
		custManager = customerService.findManagerById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(custManager);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	// 编辑客户管理层信息
	@RequestMapping("/editManager")
	public void editAddress(HttpServletRequest request,
			HttpServletResponse response, CustManager custManager)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			custManager.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateManage(custManager);
		}

		response.getWriter().print("{success:true}");
	}

	// 删除客户地址信息
	@RequestMapping("/deleteManager")
	public void deleteManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteManagerById(addSerialNo);

		response.getWriter().print("{success:true}");
	}


	/*
	 * 客户 财务报表操作
	 */

	// 财务报表摘要信息列表
	@RequestMapping("/listReport")
	public void listReport(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<ReportList> list = new ArrayList<ReportList>();

		List<Map<String, String>> reList = new ArrayList<Map<String, String>>();

		if (custId != null && !custId.equals(""))
			list = customerService.findReportListByCustId(custId);

		for (ReportList rl : list) {
			Map<String, String> map = new LinkedHashMap<String, String>();
			map.put("accountMonth", rl.getId().getAccountMonth());
			map.put("reportScope",
					DataDictionary.getInstance().getNameByCodeAndId(
							"ReportScope", rl.getId().getReportScope()));
			map.put("reportType", DataDictionary.getInstance()
					.getNameByCodeAndId("ReportType", rl.getReportType()));
			map.put("reportKind", DataDictionary.getInstance()
					.getNameByCodeAndId("ReportKind", rl.getReportKind()));
			map.put("reportCurrency", DataDictionary.getInstance()
					.getNameByCodeAndId("Currency", rl.getReportCurrency()));
			map.put("auditStatus", DataDictionary.getInstance()
					.getNameByCodeAndId("AuditStatus", rl.getAuditStatus()));
			reList.add(map);

		}

		JSONArray JO = JSONArray.fromObject(reList);
		out.print(JO);
	}

	// 获取单一财报摘要信息
	@RequestMapping("/getReport")
	public void getReport(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		ReportListId reportId = new ReportListId();

		String accountMonth = request.getParameter("accountMonth");
		String reportScopeName = request.getParameter("reportScope");
		String reportScopeCode = DataDictionary.getInstance()
				.getDataDictionary().get("ReverseReportScope")
				.get(reportScopeName).getId().getItemNo();
		String custId = request.getSession().getAttribute("custId").toString();
		if (custId != null && !custId.equals(""))
			reportId.setCustId(custId);
		reportId.setAccountMonth(accountMonth);
		reportId.setReportScope(reportScopeCode);

		ReportList reportList = new ReportList();

		reportList = customerService.findReportListById(reportId);

		JSONObject JO = JSONObject.fromObject(reportList);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(JO);

	}

	// 编辑财报摘要信息
	@RequestMapping("/editReportAbs")
	public void editReportAbs(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		ReportList reportList = new ReportList();
		ReportListId reportId = new ReportListId();

		String reportType = request.getParameter("reportType");
		String accountMonth = request.getParameter("accountMonth");
		String reportScope = request.getParameter("reportScope");
		String reportKind = request.getParameter("reportKind");
		String reportCurrency = request.getParameter("reportCurrency");
		String auditStatus = request.getParameter("auditStatus");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			reportId.setCustId(request.getSession().getAttribute("custId")
					.toString());
			reportId.setAccountMonth(accountMonth);
			reportId.setReportScope(reportScope);

			reportList = customerService.findReportListById(reportId);

			reportList.setReportType(reportType);
			reportList.setReportKind(reportKind);
			reportList.setReportCurrency(reportCurrency);
			reportList.setAuditStatus(auditStatus);
			reportList.setUpdateDate(CalendarUtil.getCurrentDateStr());

			customerService.updateReportList(reportList);
		}
		response.getWriter().print("{success:true}");
	}

	// 删除财报

	// 查看财报详情
	@RequestMapping("/checkReportDetail")
	public String checkReportDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		ReportList report = new ReportList();
		ReportListId reportId = new ReportListId();

		String accountMonth = request.getParameter("accountMonth");
		String reportScopeName = request.getParameter("reportScope");
		String reportScopeCode = DataDictionary.getInstance()
				.getDataDictionary().get("ReverseReportScope")
				.get(reportScopeName).getId().getItemNo();
		String custId = request.getSession().getAttribute("custId").toString();
		if (custId != null && !custId.equals(""))
			reportId.setCustId(custId);
		reportId.setAccountMonth(accountMonth);
		reportId.setReportScope(reportScopeCode);

		report = customerService.findReportListById(reportId);
		request.getSession().removeAttribute("report");
		request.getSession().setAttribute("report", report);

		if (custId != null && !custId.equals("")) {
			CustInfo custInfo = customerService.findCustByCustId(custId);
			// 企业法人
			if (custInfo.getCustType().equals(CustInfo.CUSTYPE_ENTER)) {
				// 大中企业
				if (report.getReportType().equals("01")) {
					List<ReportData> reportDataListAsset = customerService.findSortedEnterAsset(report);
					List<ReportData> reportDataListDebt = customerService.findSortedEnterDebt(report);
					if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.ENTER_ASSET_NUM + SheetNum.ENTER_DEBT_NUM) {
						model.addAttribute("AssetDataList",reportDataListAsset);
						model.addAttribute("DebtDataList",reportDataListDebt);
						// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
						model.addAttribute("flag", 1);
					} else {
						model.addAttribute("flag", 0);
					}

					return "customer/AddCustomer_Balance";
				}
				// 小企业
				else if (report.getReportType().equals("02")) {
					List<ReportData> reportDataListAsset = customerService.findSortedSmallAsset(report);
					List<ReportData> reportDataListDebt = customerService.findSortedSmallDebt(report);
					if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.SMALL_ASSET_NUM + SheetNum.SMALL_DEBT_NUM) {
						model.addAttribute("SmallAssetDataList",reportDataListAsset);
						model.addAttribute("SmallDebtDataList",reportDataListDebt);
						// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
						model.addAttribute("flag", 1);
					} else {
						model.addAttribute("flag", 0);
					}

					return "customer/AddCustomer_SmallBalance";
				}

			}
			// 事业单位
			else if (custInfo.getCustType().equals(CustInfo.CUSTYPE_PUBLIC)) {
				List<ReportData> reportDataListAsset = customerService.findSortedPublicAsset(report);
				List<ReportData> reportDataListDebt = customerService.findSortedPublicDebt(report);
				if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.PUBLIC_ASSET_NUM + SheetNum.PUBLIC_DEBT_NUM) {
					model.addAttribute("PublicAssetDataList",reportDataListAsset);
					model.addAttribute("PublicDebtDataList",reportDataListDebt);

					// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
					model.addAttribute("flag", 1);
				} else {
					model.addAttribute("flag", 0);
				}

				return "customer/AddCustomer_PublicBalance";
			}
			// 金融同业
			else if (custInfo.getCustType().equals(CustInfo.CUSTYPE_FINAN)) {
				List<ReportData> reportDataListAsset = customerService.findSortedFinanAsset(report);
				List<ReportData> reportDataListDebt = customerService.findSortedFinanDebt(report);
				if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.FINAN_ASSET_NUM + SheetNum.FINAN_DEBT_NUM) {
					model.addAttribute("FinanAssetDataList",reportDataListAsset);
					model.addAttribute("FinanDebtDataList",reportDataListDebt);

					// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
					model.addAttribute("flag", 1);
				} else {
					model.addAttribute("flag", 0);
				}
				return "customer/AddCustomer_FinanBalance";

			} else {
				return "customer/AddCustomer_Balance";
			}
		} else {
		}
		return "customer/AddCustomer_Balance";
	}

	// 新增财报
	@RequestMapping("/addReport")
	public String addReport(HttpServletRequest request,
			HttpServletResponse response, ReportList reportList, Model model) {

		if (reportList != null) {

			ReportListId reportId = new ReportListId();

			String accountMonth = request.getParameter("accountMonth");
			String reportScope = request.getParameter("reportScope");
			String custId = request.getSession().getAttribute("custId").toString();

			ReportList hisReportList = customerService.findReportListById(new ReportListId(custId,accountMonth,reportScope));
			if (custId != null && !custId.equals(""))
				reportId.setCustId(custId);
			reportId.setAccountMonth(accountMonth);
			reportId.setReportScope(reportScope);

			reportList.setId(reportId);
			reportList.setInputDate(CalendarUtil.getCurrentDateStr());
			reportList.setUpdateDate(CalendarUtil.getCurrentDateStr());

			request.getSession().removeAttribute("report");
			request.getSession().setAttribute("report", reportList);

			customerService.insertReport(reportList);
		}

		String custId = request.getSession().getAttribute("custId").toString();
		if (custId != null && !custId.equals("")) {
			CustInfo custInfo = customerService.findCustByCustId(custId);
			if (custInfo.getCustType().equals(CustInfo.CUSTYPE_ENTER)) {
				if (reportList.getReportType().equals("01"))
					return "customer/AddCustomer_Balance";
				else if (reportList.getReportType().equals("02")) {
					return "customer/AddCustomer_SmallBalance";
				} else {
					return "customer/AddCustomer_Balance";
				}
			} else if (custInfo.getCustType().equals(CustInfo.CUSTYPE_PUBLIC)) {
				return "customer/AddCustomer_PublicBalance";
			} else if (custInfo.getCustType().equals(CustInfo.CUSTYPE_FINAN)) {
				return "customer/AddCustomer_FinanBalance";
			} else {
				return "customer/AddCustomer_Balance";
			}
		} else {
			return "customer/AddCustomer_Balance";
		}
	}
	
	/*
	 * 报表列表——删除财报
	 */
	@RequestMapping("/deleteReport")
	public void deleteReport(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		ReportListId reportId = new ReportListId();
		String accountMonth = request.getParameter("accountMonth");
		String reportScopeName = request.getParameter("reportScope");
		String reportScopeCode = DataDictionary.getInstance()
				.getDataDictionary().get("ReverseReportScope")
				.get(reportScopeName).getId().getItemNo();
		String custId = request.getSession().getAttribute("custId").toString();
		if (custId != null && !custId.equals(""))
			reportId.setCustId(custId);
		reportId.setAccountMonth(accountMonth);
		reportId.setReportScope(reportScopeCode);

		ReportList reportList = new ReportList();

		reportList = customerService.findReportListById(reportId);
		
		if(reportList != null)customerService.deleteReport(reportList);
		
		response.getWriter().print("{success:false}");

	}
	

	/*
	 * 客户 我行存贷款操作
	 */

	// 获取我行存款列表
	@RequestMapping("/listDeposit")
	public void listDeposit(HttpServletRequest request,
			HttpServletResponse response, String custId, String type)
			throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<BankCustDeposit> list = new ArrayList<BankCustDeposit>();

		if (custId != null && !custId.equals(""))
			list = customerService.findDepositByCustIdAndType(custId, type);

		for (BankCustDeposit cm : list) {
			cm.setAccountType(DataDictionary.getInstance().getNameByCodeAndId(
					"AccountType", cm.getAccountType()));
			cm.setBankType(DataDictionary.getInstance().getNameByCodeAndId(
					"BankType", cm.getBankType()));
			cm.setCurrencyType(DataDictionary.getInstance().getNameByCodeAndId(
					"Currency", cm.getCurrencyType()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	
	//获取关联客户列表
	@RequestMapping("/listAssociateAccount")
	public void listAssociateAccount(HttpServletRequest request,
			HttpServletResponse response, String custId, String type)
			throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<AssociateAccount> list = new ArrayList<AssociateAccount>();

		if (custId != null && !custId.equals(""))
			list = customerService.findAssociateAccountByCustId(custId);

		for (AssociateAccount cm : list) {
			cm.setAccountType(DataDictionary.getInstance().getNameByCodeAndId(
					"AccountType", cm.getAccountType()));
			cm.setCurrency(DataDictionary.getInstance().getNameByCodeAndId(
					"Currency", cm.getCurrency()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}
	
	/*
	 * 获取我行贷款列表
	 */
	@RequestMapping("/listLoan")
	public void listLoan(HttpServletRequest request,
			HttpServletResponse response, String custId, String type)
			throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<BankCustLoan> list = new ArrayList<BankCustLoan>();

		if (custId != null && !custId.equals(""))
			list = customerService.findLoanByCustIdAndType(custId, type);
		
		for(BankCustLoan loan : list){
			if(loan.getBusinessType()!=null && !loan.getBankType().equals(""))
				loan.setBusinessType(DataDictionary.getInstance().getNameByCodeAndId("DebtType", loan.getBusinessType()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	/*
	 * 客户 对外担保情况操作
	 */

	// 获取客户担保列表
	@RequestMapping("/listGuarantee")
	public void listGuarantee(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<BankCustGuarantee> list = new ArrayList<BankCustGuarantee>();

		if (custId != null && !custId.equals(""))
			list = customerService.findGuaranteeByCustId(custId);

		for(BankCustGuarantee bcg : list){
			bcg.setGuaranteeType(DataDictionary.getInstance().getNameByCodeAndId("guaranteeType", bcg.getGuaranteeType()));
		}

		JSONArray ja = JSONArray.fromObject(list);
		out.print(ja);
	}

	// 新增客户对外担保情况
	@RequestMapping("/addGuarantee")
	public void addGuarantee(HttpServletRequest request,
			HttpServletResponse response, BankCustGuarantee bankCustGuarantee)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			bankCustGuarantee.setCustId(request.getSession()
					.getAttribute("custId").toString());
		bankCustGuarantee.setInputDate(CalendarUtil.getCurrentDateStr());
		bankCustGuarantee.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.insertGuarantee(bankCustGuarantee);

		response.getWriter().print("{success:true}");

	}

	// 获取一项客户对外担保信息
	@RequestMapping("/getGuarantee")
	public void getGuarantee(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String serialNo = request.getParameter("serialNo");
		BankCustGuarantee bankCustGuarantee = new BankCustGuarantee();
		bankCustGuarantee = customerService.findGuaranteeById(serialNo);

		JSONObject JO = JSONObject.fromObject(bankCustGuarantee);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(JO);
	}

	// 编辑客户对外担保
	@RequestMapping("/editGuarantee")
	public void editGuarantee(HttpServletRequest request,
			HttpServletResponse response, BankCustGuarantee bankCustGuarantee)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			bankCustGuarantee.setCustId(request.getSession()
					.getAttribute("custId").toString());
			customerService.updateGuarantee(bankCustGuarantee);
		}
		response.getWriter().print("{success:true}");
	}

	// 删除客户对外担保情况
	@RequestMapping("/deleteGuarantee")
	public void deleteGuarantee(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String serialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteGuaranteeById(serialNo);

		response.getWriter().print("{success:true}");

	}

	/*
	 * 添加客户 首先移除该客户的session
	 */
	@RequestMapping("/addCustomer")
	public String addCustomer(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		request.getSession().removeAttribute("custId");
		String hidden = "hidden";
		model.addAttribute("isExist", hidden);
		model.addAttribute("CustType", request.getParameter("CustType"));

		log.info("add Customer:　" + model.containsAttribute("isExist") + "");

		return "customer/AddCustomer_Main";
	}

	/*
	 * 添加客户信息 地址信息
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveAddress")
	public String saveAddress(HttpServletRequest request,
			HttpServletResponse response, CustAddress custAddress, Model model) {
		String[] selected = request.getParameterValues("selectAddress");

		// 移除未被选中的地址
		Object custId = request.getSession().getAttribute("custId");
		List<CustAddress> custAddressList = custAddressDao.findByCustId(custId);
		List<String> selectedList = new ArrayList<String>();
		for (String s : selected) {
			selectedList.add(s);
		}
		for (CustAddress ca : custAddressList) {
			if (!selectedList.contains(ca.getSerialNo())) {
				log.info("移除未被选中的地址" + ca.getAddressDesc());
				custAddressDao.delete(ca);
			}
		}

		model.addAttribute(
				"custAddressList",
				customerService.findAllByCustId(request.getSession()
						.getAttribute("custId").toString()));

		return "customer/CheckCustomer_Address";
	}

	/*
	 * 添加客户信息 经营情况
	 */
	@RequestMapping("/overview")
	public String overview(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String custId = request.getSession().getAttribute("custId").toString();
		model.addAttribute("custInfo", dispatchService.getCustomerInfo(custId));
		model.addAttribute("custAddressList",
				dispatchService.getCustomerAddress(custId));
		model.addAttribute("custManagerList",
				dispatchService.getCustomerManager(custId));
		model.addAttribute("custStockholderList",
				dispatchService.getCustomerStockholder(custId));
		
		return "customer/CheckCustomer_Overview";
	}

	/*
	 * 添加客户信息 保存管理层情况
	 */

	@SuppressWarnings("unchecked")
	@RequestMapping("/saveManager")
	public String saveManager(HttpServletRequest request,
			HttpServletResponse response, CustManager custManager, Model model) {
		String[] selected = request.getParameterValues("selectManager");

		// 移除未被选中的高管
		Object custId = request.getSession().getAttribute("custId");
		List<CustManager> custManagerList = custManagerDao.findByCustId(custId);
		List<String> selectedList = new ArrayList<String>();
		for (String s : selected) {
			selectedList.add(s);
		}
		for (CustManager cm : custManagerList) {
			if (!selectedList.contains(cm.getSerialNo())) {
				log.info("移除未被选中的高管" + cm.getManagerName());
				custManagerDao.delete(cm);
			}
		}

		model.addAttribute(
				"custManagerList",
				customerService.findManagerByCustId(request.getSession()
						.getAttribute("custId").toString()));

		return "customer/CheckCustomer_Management";
	}

	/*
	 * 客户重大事项操作
	 */

	// 客户重大事项列表
	@RequestMapping("/listEvents")
	public void listEvents(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<CustEvents> list = new ArrayList<CustEvents>();

		if (custId != null && !custId.equals(""))
			list = customerService.findEventsByCustId(custId);

		for (CustEvents ce : list) {
			ce.setEventExtraType(DataDictionary.getInstance().getNameByCodeAndId(
					"EventExtraType", ce.getEventExtraType()));
		}

		JSONArray ja = JSONArray.fromObject(list);
		out.print(ja);
	}

	// 新增客户重大事项
	@RequestMapping("/addEvents")
	public void addEvents(HttpServletRequest request,
			HttpServletResponse response, CustEvents custEvents)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			custEvents.setCustId(request.getSession().getAttribute("custId")
					.toString());
		custEvents.setInputDate(CalendarUtil.getCurrentDateStr());
		custEvents.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.insertCustEvents(custEvents);

		response.getWriter().print("{success:true}");

	}

	// 获取单一客户重大事项信息
	@RequestMapping("/getEvents")
	public void getEvents(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String serialNo = request.getParameter("serialNo");
		CustEvents custEvent = new CustEvents();
		custEvent = customerService.findEventsById(serialNo);

		JSONObject eventJO = JSONObject.fromObject(custEvent);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(eventJO);
	}

	// 编辑客户重大事项信息
	@RequestMapping("/editEvents")
	public void editEvents(HttpServletRequest request,
			HttpServletResponse response, CustEvents custEvent)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			custEvent.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateEvent(custEvent);
		}
		response.getWriter().print("{success:true}");
	}

	// 删除客户重大事项信息
	@RequestMapping("/deleteEvents")
	public void deleteEvents(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteEventsById(addSerialNo);
		response.getWriter().print("{success:true}");
	}

	/*
	 * 添加客户信息 客户我行存款
	 */
	@RequestMapping("/addDeposit")
	public void addDeposit(HttpServletRequest request,
			HttpServletResponse response, BankCustDeposit bankCustDeposit,
			Model model) throws IOException {

		if (bankCustDeposit != null) {
			bankCustDeposit.setInputDate(CalendarUtil.getCurrentDateStr());
			bankCustDeposit.setUpdateDate(CalendarUtil.getCurrentDateStr());
			bankCustDeposit.setCustId(request.getSession()
					.getAttribute("custId").toString());
			if (request.getParameter("bankType") != null)
				bankCustDeposit.setBankType(request.getParameter("bankType"));

			customerService.insertDeposit(bankCustDeposit);
		}

		response.getWriter().print("{success:true}");
	}

	//添加关联账户
	@RequestMapping("/addAssociateAccount")
	public void addAssociateAccount(HttpServletRequest request,
			HttpServletResponse response, AssociateAccount associateAccount,
			Model model) throws IOException {

		if (associateAccount != null) {
			associateAccount.setInputDate(CalendarUtil.getCurrentDateStr());
			associateAccount.setUpdateDate(CalendarUtil.getCurrentDateStr());
			associateAccount.setCustId(request.getSession()
					.getAttribute("custId").toString());
			
			customerService.insertAssociateAccount(associateAccount);
		}

		response.getWriter().print("{success:true}");
	}
	
	/*
	 * 获取存款信息
	 */
	@RequestMapping("/getDeposit")
	public void getDeposit(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		BankCustDeposit bankCustDeposit = new BankCustDeposit();
		bankCustDeposit = customerService.findDepositById(addSerialNo);
		/*
		 * List<CustAddress> al = new ArrayList<CustAddress>();
		 * al.add(custAddress); JSONArray addressJO = JSONArray.fromObject(al);
		 */
		// custAddress.setCityCode(DataDictionary.getInstance().getNameByCodeAndId("RegionCode",
		// custAddress.getCityCode()));
		JSONObject addressJO = JSONObject.fromObject(bankCustDeposit);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	/*
	 * 获取关联账户
	 */
	@RequestMapping("/getAssociateAccount")
	public void getAssociateAccount(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		AssociateAccount associateAccount = new AssociateAccount();
		associateAccount = customerService.findAssociateAccountById(addSerialNo);
		/*
		 * List<CustAddress> al = new ArrayList<CustAddress>();
		 * al.add(custAddress); JSONArray addressJO = JSONArray.fromObject(al);
		 */
		// custAddress.setCityCode(DataDictionary.getInstance().getNameByCodeAndId("RegionCode",
		// custAddress.getCityCode()));
		JSONObject addressJO = JSONObject.fromObject(associateAccount);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}
	
	/*
	 * 删除我行存款
	 */
	@RequestMapping("/deleteDeposit")
	public void deleteDeposit(HttpServletRequest request,
			HttpServletResponse response) {

		String addSerialNo = request.getParameter("serialNo");

		// return "customer/Customer_Inner";

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteDepositById(addSerialNo);

	}

	/*
	 * 编辑我行存款
	 */
	@RequestMapping("/editDeposit")
	public void editDeposit(HttpServletRequest request,
			HttpServletResponse response, BankCustDeposit bankCustDeposit) {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			bankCustDeposit.setCustId(request.getSession()
					.getAttribute("custId").toString());
			bankCustDeposit.setUpdateDate(CalendarUtil.getCurrentDateStr());
			bankCustDeposit.setSerialNo(request.getParameter("serialNo"));
			if (request.getParameter("bankType") != null)
				bankCustDeposit.setBankType(request.getParameter("bankType"));
			customerService.updateDeposit(bankCustDeposit);
		}

		// return "customer/AddCustomer_Address";
	}
	

	/*
	 * 编辑关联账户
	 */
	@RequestMapping("/editAssociateAccount")
	public void editAssociateAccount(HttpServletRequest request,
			HttpServletResponse response, AssociateAccount associateAccount) {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			associateAccount.setCustId(request.getSession()
					.getAttribute("custId").toString());
			associateAccount.setUpdateDate(CalendarUtil.getCurrentDateStr());
			associateAccount.setSerialNo(request.getParameter("serialNo"));
			customerService.updateAssociateAccount(associateAccount);
		}
	}
	
	/*
	 * 删除关联客户
	 */
	@RequestMapping("/deleteAssociateAccount")
	public void deleteAssociateAccount(HttpServletRequest request,
			HttpServletResponse response) {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteAssociateAccountById(addSerialNo);
	}


	/*
	 * 添加客户信息 保存客户我行存款
	 */

	@SuppressWarnings("unchecked")
	@RequestMapping("/saveDeposit")
	public String saveDeposit(HttpServletRequest request,
			HttpServletResponse response, BankCustDeposit bankCustDeposit,
			Model model) {
		String[] selected = request.getParameterValues("selectDeposit");

		// 移除未被选中的事件
		Object custId = request.getSession().getAttribute("custId");
		List<BankCustDeposit> custDepositList = bankCustDepositDao
				.findByCustId(custId);
		List<String> selectedList = new ArrayList<String>();
		for (String s : selected) {
			selectedList.add(s);
		}
		for (BankCustDeposit bd : custDepositList) {
			if (!selectedList.contains(bd.getSerialNo())) {
				log.info("移除未被选中的事件" + bd.getOrgId());
				bankCustDepositDao.delete(bd);
			}
		}

		model.addAttribute(
				"custDepositList",
				customerService.findDepositByCustId(request.getSession()
						.getAttribute("custId").toString()));
		model.addAttribute(
				"custLoanList",
				customerService.findLoanByCustId(request.getSession()
						.getAttribute("custId").toString()));

		return "customer/AddCustomer_InnerRelationship";
	}

	/*
	 * 添加客户信息 客户我行贷款
	 */
	@RequestMapping("/addLoan")
	public void addLoan(HttpServletRequest request,
			HttpServletResponse response, BankCustLoan bankCustLoan, Model model)
			throws IOException {

		if (bankCustLoan != null) {
			bankCustLoan.setInputDate(CalendarUtil.getCurrentDateStr());
			bankCustLoan.setUpdateDate(CalendarUtil.getCurrentDateStr());
			bankCustLoan.setCustId(request.getSession().getAttribute("custId")
					.toString());
			if (request.getParameter("orgId") != null) {
				bankCustLoan.setOrgId(request.getParameter("orgId"));
			}

			if (request.getParameter("bankType") != null)
				bankCustLoan.setBankType(request.getParameter("bankType"));
			customerService.insertLoan(bankCustLoan);
		}

		response.getWriter().print("{success:true}");
	}

	/*
	 * 获取存款信息
	 */
	@RequestMapping("/getLoan")
	public void getLoan(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		BankCustLoan bankCustLoan = new BankCustLoan();
		bankCustLoan = customerService.findLoanById(addSerialNo);
		/*
		 * List<CustAddress> al = new ArrayList<CustAddress>();
		 * al.add(custAddress); JSONArray addressJO = JSONArray.fromObject(al);
		 */
		// custAddress.setCityCode(DataDictionary.getInstance().getNameByCodeAndId("RegionCode",
		// custAddress.getCityCode()));
		JSONObject addressJO = JSONObject.fromObject(bankCustLoan);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	/*
	 * 删除我行贷款
	 */
	@RequestMapping("/deleteLoan")
	public void deleteLoan(HttpServletRequest request,
			HttpServletResponse response) {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteLoanById(addSerialNo);

	}

	/*
	 * 编辑我行贷款款
	 */
	@RequestMapping("/editLoan")
	public void editLoan(HttpServletRequest request,
			HttpServletResponse response, BankCustLoan bankCustLoan) {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			bankCustLoan.setCustId(request.getSession().getAttribute("custId")
					.toString());
			bankCustLoan.setUpdateDate(CalendarUtil.getCurrentDateStr());
			bankCustLoan.setSerialNo(request.getParameter("serialNo"));
			if (request.getParameter("bankType") != null)
				bankCustLoan.setBankType(request.getParameter("bankType"));
			customerService.updateLoan(bankCustLoan);
		}

	}

	/*
	 * 添加客户信息 保存客户我行存款
	 */

	@SuppressWarnings("unchecked")
	@RequestMapping("/saveLoan")
	public String saveLoan(HttpServletRequest request,
			HttpServletResponse response, BankCustLoan bankCustLoan, Model model) {
		String[] selected = request.getParameterValues("selectLoan");

		// 移除未被选中的事件
		Object custId = request.getSession().getAttribute("custId");
		List<BankCustLoan> custLoanList = bankCustLoanDao.findByCustId(custId);
		List<String> selectedList = new ArrayList<String>();
		for (String s : selected) {
			selectedList.add(s);
		}
		for (BankCustLoan bl : custLoanList) {
			if (!selectedList.contains(bl.getSerialNo())) {
				log.info("移除未被选中的事件" + bl.getBusinessType());
				bankCustLoanDao.delete(bl);
			}
		}

		model.addAttribute(
				"custLoanList",
				customerService.findLoanByCustId(request.getSession()
						.getAttribute("custId").toString()));
		model.addAttribute(
				"custDepositList",
				customerService.findDepositByCustId(request.getSession()
						.getAttribute("custId").toString()));

		return "customer/CheckCustomer_InnerRelationship";
	}

	/*
	 * 保存会企资产负债表
	 */
	@SuppressWarnings({ "unchecked", "null" })
	@RequestMapping("/saveBalance")
	public String saveBalance(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项，此为标记项，标记资产还是负债的中间点
			if (paraName.equals("type"))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(EnterBalanceSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));

			// rd.setDisplayFlag(ReportModelInit.getInstance());

			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(EnterBalanceSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("EBS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";

		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态并且是年报则计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				// 校验填写是否正确
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				EnterCheck ec = new EnterCheck(reportDataList);
				List<String> resultList = ec.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);// 查找前一年的年报，如果无，则置为null
					FinancialIndexCalculator fic = new FinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(EnterFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}
			}

		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListAsset = customerService.findSortedEnterAsset(report);
		List<ReportData> reportDataListDebt = customerService.findSortedEnterDebt(report);
		if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.ENTER_ASSET_NUM + SheetNum.ENTER_DEBT_NUM) {
			model.addAttribute("AssetDataList",reportDataListAsset);
			model.addAttribute("DebtDataList",reportDataListDebt);

			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_Balance";
	}

	public void insertFinancialIndex(String reportNo, Map resultMap,
			ReportList report) {

		List<ReportData> reportDataList = new ArrayList<ReportData>();
		for (Object o : resultMap.keySet()) {
			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setCustId(report.getId().getCustId());
			id.setAccountMonth(report.getId().getAccountMonth());
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					o.toString()));
			id.setReportNo(reportNo);
			id.setReportScope(report.getId().getReportScope());
			rd.setId(id);
			rd.setReportItemNo(o.toString());
			rd.setItemValue((Double) resultMap.get(o));
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(o));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					o.toString()));
//			log.info("displayNo: "+ReportModelInit.getInstance().getDisplayNoById(o.toString()));
			reportDataList.add(rd);
			
		}
		customerService.insertReportDataList(reportDataList);
		log.info("财务指标计算并写入数据库完成");
	}

	/*
	 * 保存会企损益表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveIncome")
	public String saveIncome(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(EnterIncome.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(EnterIncome.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("EIS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				EnterCheck ec = new EnterCheck(reportDataList);
				List<String> resultList = ec.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					// 查找前一年的年报，如果无，则置为null
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);
					FinancialIndexCalculator fic = new FinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(EnterFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}

			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListIncome = customerService.findSortedEnterIncome(report);
		if ( reportDataListIncome!= null && reportDataListIncome.size() == SheetNum.ENTER_INCOME_NUM) {
			model.addAttribute("EnterIncomeDataList",reportDataListIncome);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_Income";
	}

	/*
	 * 保存会企现金流量表
	 */
	@RequestMapping("/saveCashFlow")
	public String saveCashFlow(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(EnterCashFlow.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}
		//	log.info("ParamName: "+paraName+" DisplayNo: "+rd.getId().getDisplayNo());
			
			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(EnterCashFlow.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("ECF000");
		rd.setDisplayFlag(type);

		list.add(rd);
		
		for(ReportData reportData : list){
			log.info("ReportData: "+reportData.getId().getReportNo()+" "+reportData.getId().getDisplayNo()+" "+reportData.getDisplayName());
		}

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				EnterCheck ec = new EnterCheck(reportDataList);
				List<String> resultList = ec.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					// 查找前一年的年报，如果无，则置为null
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);
					FinancialIndexCalculator fic = new FinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(EnterFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}
			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListCashFlow = customerService.findSortedEnterCashFlow(report);
		if ( reportDataListCashFlow != null && reportDataListCashFlow.size() == SheetNum.ENTER_CASHFLOW_NUM) {
			model.addAttribute("EnterCashFlowDataList",reportDataListCashFlow);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_CashflowInput";
	}

	/*
	 * 保存会金融资产负债表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveFinanBalance")
	public String saveFinanBalance(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项，此为标记项，标记资产还是负债的中间点
			if (paraName.equals("type"))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(FinanBalanceSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));

			// rd.setDisplayFlag(ReportModelInit.getInstance());

			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(FinanBalanceSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("FBS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";

		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态并且是年报则计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService
						.findRecentReportData(report);
				// 查找前一年的年报，如果无，则置为null
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
				FinanVerify pv = new FinanVerify(reportDataList);
				if (pv.getResultList() != null && pv.getResultList().size() > 0) {
					log.info("请检查您的报表");
				} else {
					FinanFinancialIndexCalculator fic = new FinanFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(FinanFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				}
			}

		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListAsset = customerService.findSortedFinanAsset(report);
		List<ReportData> reportDataListDebt = customerService.findSortedFinanDebt(report);
		if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.FINAN_ASSET_NUM + SheetNum.FINAN_DEBT_NUM) {
			model.addAttribute("FinanAssetDataList",reportDataListAsset);
			model.addAttribute("FinanDebtDataList",reportDataListDebt);

			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_FinanBalance";
	}

	/*
	 * 保存会金融损益表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveFinanIncome")
	public String saveFinanIncome(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(FinanIncomeSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(FinanIncomeSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("FIS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService
						.findRecentReportData(report);
				// 查找前一年的年报，如果无，则置为null
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
				FinanVerify pv = new FinanVerify(reportDataList);
				if (pv.getResultList() != null && pv.getResultList().size() > 0) {
					log.info("请检查您的报表");
				} else {
					FinanFinancialIndexCalculator fic = new FinanFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(FinanFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				}

			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListIncome = customerService.findSortedFinanIncome(report);
		if ( reportDataListIncome!= null && reportDataListIncome.size() == SheetNum.FINAN_INCOME_NUM) {
			model.addAttribute("FinanIncomeDataList",reportDataListIncome);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_FinanIncome";
	}

	/*
	 * 保存会金融财务比率指标表手工输入
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveFinanFinancialIndexInput")
	public String saveFinanFinancialIndexInput(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(FinanFinancialIndexSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(FinanFinancialIndexSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("FFR000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService
						.findRecentReportData(report);
				// 查找前一年的年报，如果无，则置为null
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
				FinanVerify pv = new FinanVerify(reportDataList);
				if (pv.getResultList() != null && pv.getResultList().size() > 0) {
					log.info("请检查您的报表");
				} else {
					FinanFinancialIndexCalculator fic = new FinanFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(FinanFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				}
			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListIndex = customerService.findSortedFinanFinancialIndexInput(report);
		if ( reportDataListIndex != null && reportDataListIndex.size() == SheetNum.FINAN_INPUTINDEX_NUM) {
			model.addAttribute("FinanFinancialIndexInputDataList",reportDataListIndex);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_FinanFinancialIndexInput";
	}

	/*
	 * 保存小企业资产负债表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveSmallBalance")
	public String saveSmallBalance(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项，此为标记项，标记资产还是负债的中间点
			if (paraName.equals("type"))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(SmallBalanceSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));

			// rd.setDisplayFlag(ReportModelInit.getInstance());

			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(SmallBalanceSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("SBS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";

		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态并且是年报则计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				SmallCheck sc = new SmallCheck(reportDataList);
				List<String> resultList = sc.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);// 查找前一年的年报，如果无，则置为null
					SmallFinancialIndexCalculator fic = new SmallFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(SmallFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}
			}

		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListAsset = customerService.findSortedSmallAsset(report);
		List<ReportData> reportDataListDebt = customerService.findSortedSmallDebt(report);
		if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.SMALL_ASSET_NUM + SheetNum.SMALL_DEBT_NUM) {
			model.addAttribute("SmallAssetDataList",reportDataListAsset);
			model.addAttribute("SmallDebtDataList",reportDataListDebt);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_SmallBalance";
	}

	/*
	 * 保存小企业现金流量表
	 */
	@RequestMapping("/saveSmallCashFlow")
	public String saveSmallCashFlow(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(SmallCashFlowSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(SmallCashFlowSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("SCF000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				SmallCheck sc = new SmallCheck(reportDataList);
				List<String> resultList = sc.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);// 查找前一年的年报，如果无，则置为null
					SmallFinancialIndexCalculator fic = new SmallFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(SmallFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}
			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListCashFlow = customerService.findSortedSmallCashFlow(report);
		if (reportDataListCashFlow != null && reportDataListCashFlow.size() == SheetNum.SMALL_CASHFLOW_NUM) {
			model.addAttribute("SmallCashFlowDataList",reportDataListCashFlow);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_SmallCashflow";
	}

	/*
	 * 客户列表——删除客户
	 */
	@RequestMapping("/deleteCustomer")
	public void deleteCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custId = request.getParameter("CustId");
		CustInfo custInfo = custInfoDao.findById(custId);
		if(custInfo!=null){
			custInfoDao.delete(custInfo);
		}else{
			IndInfo indInfo = indInfoDAO.findById(custId);
			if(indInfo!=null)
				indInfoDAO.delete(indInfo);
		}
		
		response.getWriter().print("{success:false}");

	}

	/*
	 * 获取零售客户列表
	 */
	@RequestMapping("/listRetailCustomer")
	public void listRetailCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		List<IndInfo> myCustList = new ArrayList<IndInfo>();

		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		
		myCustList = customerService.findAllIndCustomer(userId,orgId);
		// myCustList.subList(fromIndex, toIndex)
		// 如果custType为空，查找所有属于该用户的custList
		// TODO
		List<IndInfo> pageCustList;

		// 分页,该页size未超过剩余size
		if (myCustList.size() > start + limit)
			pageCustList = myCustList.subList(start, limit + start);
		else
			pageCustList = myCustList.subList(start, myCustList.size());
		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("total", myCustList.size());
		myCustMap.put("customer", pageCustList);
		JSONObject myCustJO = JSONObject.fromObject(myCustMap);
		out.print(myCustJO);
		log.info("retailCustomer: "+myCustJO);
	}


	//查询客户
	@RequestMapping("/findIndCustomer")
	public void findIndCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String custType = request.getParameter("CustType");
		String condition = request.getParameter("condition");
		String keyword = request.getParameter("keyword");
		
		List<IndInfo> list = new ArrayList<IndInfo>(); 
		if("custId".equals(condition))condition = "custNo";
		list = customerService.findIndCustomerByIdAndName(condition, keyword);
		//分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		
		List<IndInfo> pageList;
		if(list.size()>start+limit)
			pageList = list.subList(start, limit+start); 
		else
			pageList = list.subList(start,list.size());
		
		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("success", "true");
		myCustMap.put("total", list.size());
		myCustMap.put("resultData", pageList);
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
		
		log.info("JSON data: "+myCustJO);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(myCustJO);
		
	}
	
	@RequestMapping("/checkRetailCustomer")
	public void checkRetailCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custName = "liupengwangfuqiang";
		String custNo = request.getParameter("CustNo");
		String idtype = request.getParameter("idtype");
		String idno = request.getParameter("idno");
		int isExit = 0;

		if (customerService.isRetailCustExist(custName, custNo,idtype, idno)) {
			isExit = 1;
			response.getWriter().print(isExit);
		} else {
			isExit = 0;
			response.getWriter().print(isExit);
		}

	}

	/*
	 * 添加客户 添加客户的最基本信息
	 */
	@RequestMapping("/addRetailMain")
	public String addRetailMain(HttpServletRequest request,
			HttpServletResponse response, IndInfo custInfo, Model model)
			throws IOException {

		// 设置当前时间
		custInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		
		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		
		custInfo.setInputUserId(userId);
		custInfo.setInputOrgId(orgId);

		customerService.insertRetailGeneral(custInfo);
		model.addAttribute("CustInfo", custInfo);

		request.getSession().removeAttribute("indCustId");
		request.getSession().setAttribute("indCustId", custInfo.getCustId());
		request.getSession().removeAttribute("indCustNo");
		request.getSession().setAttribute("indCustNo", custInfo.getCustNo());
		request.getSession().removeAttribute("indCustName");
		request.getSession().setAttribute("indCustName", custInfo.getCustName());

		log.info("DataDictionary: " + DataDictionary.getInstance());
		// response.getWriter().print("{success:true}");

		return "customer/Customer_IndGeneral";
	}

	// check零售客户
	@RequestMapping("/checkIndCustomer")
	public String checkIndCustomer(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		request.getSession().removeAttribute("indCustId");

		String custId = request.getParameter("CustId");
		IndInfo selectedCust = new IndInfo();
		selectedCust = customerService.findIndCustByCustId(custId);
		model.addAttribute("CustInfo", selectedCust);
		request.getSession().removeAttribute("indCustId");
		request.getSession().setAttribute("indCustId", custId);
		request.getSession().removeAttribute("indCustNo");
		request.getSession().setAttribute("indCustNo", selectedCust.getCustNo());
		request.getSession().removeAttribute("indCustName");
		request.getSession().setAttribute("indCustName",
				selectedCust.getCustName());

		return "customer/Customer_IndGeneral";
	}

	@RequestMapping("/getIndGeneral")
	public void getIndGeneral(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		IndInfo custInfo = new IndInfo();
		custInfo = customerService.findIndCustByCustId(custId);

		Map<String, String> industry = Industry.getInstance().getIndustry();
		if(custInfo.getIndustry()!=null && !custInfo.getIndustry().equals("")){
			custInfo.setIndustryTypeLevelOne(industry.get(custInfo
				.getIndustry().substring(0, 1)));
			custInfo.setIndustryTypeLevelTwo(industry.get(custInfo
				.getIndustry()));
		}
		Map map = new HashMap();
		map.put("success", true);
		map.put("data", custInfo);

		JSONObject custJO;
		// 如果是从添加保证人页面进入
		if (request.getParameter("isGuarantor") != null) {
			custJO = JSONObject.fromObject(custInfo);
		} else {
			custJO = JSONObject.fromObject(map);
		}
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(custJO);
	}

	// 编辑基本信息和职业信息都复用此方法
	@RequestMapping("/editIndGeneral")
	public void editIndGeneral(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		IndInfo custInfo = new IndInfo();
		custInfo = customerService.findIndCustByCustId(custId);
		String isOperation = request.getParameter("isOperation");
		

		if (custInfo != null && isOperation != null && !isOperation.equals("1")) {
			custInfo.setCustNo(request.getParameter("custNo"));
			custInfo.setSexType(request.getParameter("sexType"));
			custInfo.setBirthDate(request.getParameter("birthDate"));
			custInfo.setGrade(request.getParameter("grade"));
			custInfo.setDegree(request.getParameter("degree"));
			custInfo.setZipAdd(request.getParameter("zipAdd"));
			custInfo.setZipcode(request.getParameter("zipcode"));
			custInfo.setHouseAdd(request.getParameter("houseAdd"));
			custInfo.setWorktel(request.getParameter("worktel"));
			custInfo.setCellPhoneNo(request.getParameter("cellPhoneNo"));
			custInfo.setEmailAdd(request.getParameter("emailAdd"));
			custInfo.setHrAdd(request.getParameter("hrAdd"));
			custInfo.setMstatus(request.getParameter("mstatus"));
			custInfo.setMateName(request.getParameter("mateName"));
			custInfo.setMatework(request.getParameter("matework"));
			custInfo.setResideAdd(request.getParameter("resideAdd"));
			custInfo.setResideState(request.getParameter("resideState"));
			custInfo.setMatetel(request.getParameter("matetel"));
			
			String socialSecurity1 = request.getParameter("socialSecurity1");
			if(socialSecurity1 == null) socialSecurity1 = "0";
			String socialSecurity2 = request.getParameter("socialSecurity2");
			if(socialSecurity2 == null) socialSecurity2 = "0";
			String socialSecurity3 = request.getParameter("socialSecurity3");
			if(socialSecurity3 == null) socialSecurity3 = "0";
			String socialSecurity4 = request.getParameter("socialSecurity4");
			if(socialSecurity4 == null) socialSecurity4 = "0";
			String socialSecurity5 = request.getParameter("socialSecurity5");
			if(socialSecurity5 == null) socialSecurity5 = "0";
			String socialSecurity6 = request.getParameter("socialSecurity6");
			if(socialSecurity6 == null) socialSecurity6 = "0";
			String socialSecurity7 = request.getParameter("socialSecurity7");
			if(socialSecurity7 == null) socialSecurity7 = "0";
			
			custInfo.setSocialSecurity1(socialSecurity1);
			custInfo.setSocialSecurity2(socialSecurity2);
			custInfo.setSocialSecurity3(socialSecurity3);
			custInfo.setSocialSecurity4(socialSecurity4);
			custInfo.setSocialSecurity5(socialSecurity5);
			custInfo.setSocialSecurity6(socialSecurity6);
			custInfo.setSocialSecurity7(socialSecurity7);
			custInfo.setIsPeasant(request.getParameter("isPeasant"));
			custInfo.setRelationshipDate(request.getParameter("relationshipDate"));

			String industry = custInfo.getIndustry();
			if(industry != null && industry.contains(" "))custInfo.setIndustry(industry.substring(0,industry.indexOf(" ")));
			customerService.updateIndCustInfo(custInfo);
			
			request.getSession().removeAttribute("indCustNo");
			request.getSession().setAttribute("indCustNo", custInfo.getCustNo());
			
			
		} else {
			custInfo.setCoName(request.getParameter("coName"));
			custInfo.setCoAdd(request.getParameter("coAdd"));
			custInfo.setCoZipCode(request.getParameter("coZipCode"));
			custInfo.setIndustry(request.getParameter("industryTypeLevelTwo"));
			custInfo.setJobType(request.getParameter("jobType"));
			custInfo.setJobPosition(request.getParameter("jobPosition"));
			custInfo.setJobTitle(request.getParameter("jobTitle"));
			custInfo.setYearIncome(Double.valueOf(request
					.getParameter("yearIncome")));
			custInfo.setStartYear(request.getParameter("startYear"));

			String industry = custInfo.getIndustry();
			if(industry.contains(" ")){
				int i = industry.indexOf(" ");
				custInfo.setIndustry(industry.substring(0,i));
			}
				
			customerService.updateIndCustInfo(custInfo);
		}

		response.getWriter().print("{success:true}");
	}

	@RequestMapping("/getIndOccard")
	public void getIndOccard(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId").toString();
		String custNo = request.getSession().getAttribute("indCustNo").toString();
		IndOccard custInfo;
		custInfo = customerService.findIndOccardByCustId(custId);

		if (custInfo == null) {
			custInfo = new IndOccard();
			custInfo.setCustId(custId);
			custInfo.setInputDate(CalendarUtil.getCurrentDateStr());
			custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
			customerService.insertIndOccard(custInfo);
		}

		custInfo.setCustNo(custNo);
		
		Map map = new HashMap();
		map.put("success", true);
		map.put("data", custInfo);

		JSONObject custJO;
		// 如果是从添加保证人页面进入
		if (request.getParameter("isGuarantor") != null) {
			custJO = JSONObject.fromObject(custInfo);
		} else {
			custJO = JSONObject.fromObject(map);
		}
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(custJO);
	}

	// 编辑基本信息和职业信息都复用此方法
	@RequestMapping("/editIndOccard")
	public void editIndOccard(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		IndOccard custInfo;
		custInfo = customerService.findIndOccardByCustId(custId);
		if (request.getParameter("cardSum") != null) {
			log.info("cardSum: " + request.getParameter("cardSum"));
			Integer cardSum = Integer.parseInt(request.getParameter("cardSum")
					.toString());
			log.info("cardSum: " + cardSum);
			custInfo.setCardSum(cardSum);
		}

		if (!request.getParameter("cardBankSum1").equals(""))
			custInfo.setCardBankSum1(Integer.parseInt(request.getParameter(
					"cardBankSum1").toString()));
		if (request.getParameter("cardBankSum2") != null && !request.getParameter("cardBankSum2").equals(""))
			custInfo.setCardBankSum2(Integer.parseInt(request.getParameter(
					"cardBankSum2").toString()));
		if (!request.getParameter("creditSum").equals(""))
			custInfo.setCreditSum(Double.valueOf(request.getParameter(
					"creditSum").toString()));
		if (!request.getParameter("usedSum").equals(""))
			custInfo.setUsedSum(Double.valueOf(request.getParameter("usedSum")
					.toString()));
		if (!request.getParameter("dueSum").equals(""))
			custInfo.setDueSum(Double.valueOf(request.getParameter("dueSum")
					.toString()));

		custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.updateIndOccardInfo(custInfo);

		response.getWriter().print("{success:true}");
	}

	@RequestMapping("/getIndOloan")
	public void getIndOloan(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId").toString();
		String custNo = request.getSession().getAttribute("indCustNo").toString();
		IndOloan custInfo;
		custInfo = customerService.findIndOloanByCustId(custId);

		if (custInfo == null) {
			custInfo = new IndOloan();
			custInfo.setCustId(custId);
			custInfo.setInputDate(CalendarUtil.getCurrentDateStr());
			custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
			customerService.insertIndOloan(custInfo);
		}

		custInfo.setCustNo(custNo);
		Map map = new HashMap();
		map.put("success", true);
		map.put("data", custInfo);

		JSONObject custJO;
		// 如果是从添加保证人页面进入
		if (request.getParameter("isGuarantor") != null) {
			custJO = JSONObject.fromObject(custInfo);
		} else {
			custJO = JSONObject.fromObject(map);
		}
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(custJO);
	}

	// 编辑基本信息和职业信息都复用此方法
	@RequestMapping("/editIndOloan")
	public void editIndOloan(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		IndOloan custInfo;
		custInfo = customerService.findIndOloanByCustId(custId);
		if (request.getParameter("loanCount") != null) {
			log.info("loanCount: " + request.getParameter("loanCount"));
			Integer loanCount = Integer.parseInt(request.getParameter(
					"loanCount").toString());
			log.info("loanCount: " + loanCount);
			custInfo.setLoanCount(loanCount);
		}

		if (!request.getParameter("loanBankSum1").equals(""))
			custInfo.setLoanBankSum1(Integer.parseInt(request.getParameter(
					"loanBankSum1").toString()));
		if (!request.getParameter("loanBankSum1").equals(""))
			custInfo.setLoanBankSum2(Integer.parseInt(request.getParameter(
					"loanBankSum1").toString()));
		if (!request.getParameter("loanContractSum").equals(""))
			custInfo.setLoanContractSum(Double.valueOf(request.getParameter(
					"loanContractSum").toString()));
		if (!request.getParameter("loanBalance").equals(""))
			custInfo.setLoanBalance(Double.valueOf(request.getParameter(
					"loanBalance").toString()));
		if (!request.getParameter("loanSum").equals(""))
			custInfo.setLoanSum(Double.valueOf(request.getParameter("loanSum")
					.toString()));
		if (!request.getParameter("dueSum").equals(""))
			custInfo.setDueSum(Double.valueOf(request.getParameter("dueSum")
					.toString()));

		custInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());

		customerService.updateIndOloanInfo(custInfo);

		response.getWriter().print("{success:true}");
	}

	// 获取担保列表
	@RequestMapping("/listIndOguarantee")
	public void listIndOguarantee(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("indCustId").toString();
		List<IndOguarantee> list = new ArrayList<IndOguarantee>();

		if (custId != null && !custId.equals(""))
			list = customerService.findIndOguaranteeByCustId(custId);

		// 分页列表list
		List<IndOguarantee> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (IndOguarantee ca : pageList) {
			ca.setLenderIdtype(DataDictionary.getInstance().getNameByCodeAndId(
					"CertType", ca.getLenderIdtype()));

		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 地址信息
	@RequestMapping("/addIndOguarantee")
	public void addIndOguarantee(HttpServletRequest request,
			HttpServletResponse response, IndOguarantee indOguarantee,
			Model model) throws IOException {
		log.info(("custId: " + indOguarantee.getCustId()));
		indOguarantee.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		indOguarantee.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.insertIndOguarantee(indOguarantee);

		response.getWriter().print("{success:true}");
	}

	// 删除客户担保信息
	@RequestMapping("/deleteIndOguarantee")
	public void deleteIndOguarantee(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("indCustId") != null
				&& !request.getSession().getAttribute("indCustId").toString()
						.equals(""))
			customerService.deleteIndOguaranteeById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 编辑客户地址信息
	@RequestMapping("/editIndOguarantee")
	public void editIndOguarantee(HttpServletRequest request,
			HttpServletResponse response, IndOguarantee indOguarantee)
			throws IOException {
		String addSerialNo = request.getParameter("serialNo");
		// indOguarantee.setSerialNo(addSerialNo);
		indOguarantee.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		indOguarantee.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.updateIndOguarantee(indOguarantee);

		response.getWriter().print("{success:true}");
	}

	// 获取单一地址信息
	@RequestMapping("/getIndOguarantee")
	public void getIndOguarantee(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		IndOguarantee indOguarantee = new IndOguarantee();
		indOguarantee = customerService.findIndOguaranteeById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(indOguarantee);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
		log.info("json: " + addressJO);
	}

	// 获取与我行合作信息
	@RequestMapping("/listIndBank")
	public void listIndBank(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("indCustId").toString();
		List<IndBank> list = new ArrayList<IndBank>();

		if (custId != null && !custId.equals(""))
			list = customerService.findIndBankByCustId(custId);

		// 分页列表list
		List<IndBank> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (IndBank ca : pageList) {
			ca.setCustClass(DataDictionary.getInstance().getNameByCodeAndId(
					"CustClass", ca.getCustClass()));
			ca.setCustKind(DataDictionary.getInstance().getNameByCodeAndId(
					"CustKind", ca.getCustKind()));
			ca.setCardState(DataDictionary.getInstance().getNameByCodeAndId(
					"CardState", ca.getCardState()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 地址信息
	@RequestMapping("/addIndBank")
	public void addIndBank(HttpServletRequest request,
			HttpServletResponse response, IndBank indBank, Model model)
			throws IOException {
		log.info(("custId: " + indBank.getCustId()));
		indBank.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		indBank.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.insertIndBank(indBank);

		response.getWriter().print("{success:true}");
	}

	// 删除客户担保信息
	@RequestMapping("/deleteIndBank")
	public void deleteIndBank(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("indCustId") != null
				&& !request.getSession().getAttribute("indCustId").toString()
						.equals(""))
			customerService.deleteIndBankById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 编辑客户地址信息
	@RequestMapping("/editIndBank")
	public void editIndBank(HttpServletRequest request,
			HttpServletResponse response, IndBank indBank) throws IOException {
		String addSerialNo = request.getParameter("serialNo");
		// indOguarantee.setSerialNo(addSerialNo);
		indBank.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		indBank.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.updateIndBank(indBank);

		response.getWriter().print("{success:true}");
	}

	// 获取单一地址信息
	@RequestMapping("/getIndBank")
	public void getIndBank(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		IndBank indBank = new IndBank();
		indBank = customerService.findIndBankById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(indBank);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
		log.info("json: " + addressJO);
	}

	// 获取特殊交易信息
	@RequestMapping("/listSpecialTrade")
	public void listSpecialTrade(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("indCustId").toString();
		List<SpecialTrade> list = new ArrayList<SpecialTrade>();

		if (custId != null && !custId.equals(""))
			list = customerService.findSpecialTradeByCustId(custId);

		// 分页列表list
		List<SpecialTrade> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (SpecialTrade ca : pageList) {
			ca.setTradeType(DataDictionary.getInstance().getNameByCodeAndId(
					"TradeType", ca.getTradeType()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 地址信息
	@RequestMapping("/addSpecialTrade")
	public void addSpecialTrade(HttpServletRequest request,
			HttpServletResponse response, SpecialTrade specialTrade, Model model)
			throws IOException {
		log.info(("custId: " + specialTrade.getCustId()));
		specialTrade.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		specialTrade.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.insertSpecialTrade(specialTrade);

		response.getWriter().print("{success:true}");
	}

	// 删除客户担保信息
	@RequestMapping("/deleteSpecialTrade")
	public void deleteSpecialTrade(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("indCustId") != null
				&& !request.getSession().getAttribute("indCustId").toString()
						.equals(""))
			customerService.deleteSpecialTradeById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 编辑客户地址信息
	@RequestMapping("/editSpecialTrade")
	public void editSpecialTrade(HttpServletRequest request,
			HttpServletResponse response, SpecialTrade specialTrade)
			throws IOException {
		String addSerialNo = request.getParameter("serialNo");
		// indOguarantee.setSerialNo(addSerialNo);
		specialTrade.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		specialTrade.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.updateSpecialTrade(specialTrade);

		response.getWriter().print("{success:true}");
	}

	// 获取单一地址信息
	@RequestMapping("/getSpecialTrade")
	public void getSpecialTrade(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		SpecialTrade specialTrade = new SpecialTrade();
		specialTrade = customerService.findSpecialTradeById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(specialTrade);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
		log.info("json: " + addressJO);
	}

	// 获取特殊交易信息
	@RequestMapping("/listSpecialRecord")
	public void listSpecialRecord(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("indCustId").toString();
		List<SpecialRecord> list = new ArrayList<SpecialRecord>();

		if (custId != null && !custId.equals(""))
			list = customerService.findSpecialRecordByCustId(custId);

		// 分页列表list
		List<SpecialRecord> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (SpecialRecord ca : pageList) {
			ca.setRecordType(DataDictionary.getInstance().getNameByCodeAndId(
					"SpecialRecord", ca.getRecordType()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 地址信息
	@RequestMapping("/addSpecialRecord")
	public void addSpecialRecord(HttpServletRequest request,
			HttpServletResponse response, SpecialRecord specialRecord,
			Model model) throws IOException {
		log.info(("custId: " + specialRecord.getCustId()));
		specialRecord.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		specialRecord.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.insertSpecialRecord(specialRecord);

		response.getWriter().print("{success:true}");
	}

	// 删除客户担保信息
	@RequestMapping("/deleteSpecialRecord")
	public void deleteSpecialRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("indCustId") != null
				&& !request.getSession().getAttribute("indCustId").toString()
						.equals(""))
			customerService.deleteSpecialRecordById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 编辑客户地址信息
	@RequestMapping("/editSpecialRecord")
	public void editSpecialRecord(HttpServletRequest request,
			HttpServletResponse response, SpecialRecord specialRecord)
			throws IOException {
		String addSerialNo = request.getParameter("serialNo");
		// indOguarantee.setSerialNo(addSerialNo);
		specialRecord.setCustId(request.getSession().getAttribute("indCustId")
				.toString());
		specialRecord.setUpdateDate(CalendarUtil.getCurrentDateStr());
		customerService.updateSpecialRecord(specialRecord);

		response.getWriter().print("{success:true}");
	}

	// 获取单一地址信息
	@RequestMapping("/getSpecialRecord")
	public void getSpecialRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		SpecialRecord specialRecord = new SpecialRecord();
		specialRecord = customerService.findSpecialRecordById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(specialRecord);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
		log.info("json: " + addressJO);
	}

	@RequestMapping("/getIndNewestPD")
	public void getIndNewestPD(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		PdH pdh;
		pdh = customerService.findIndNewestPDByCustId(custId);

		if (pdh == null) {
			pdh = new PdH();
			pdh.setCustId(custId);
			customerService.insertIndPdH(pdh);
		}

		Map map = new HashMap();
		map.put("success", true);
		map.put("data", pdh);

		JSONObject custJO;
		// 如果是从添加保证人页面进入
		if (request.getParameter("isGuarantor") != null) {
			custJO = JSONObject.fromObject(pdh);
		} else {
			custJO = JSONObject.fromObject(map);
		}
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(custJO);
	}

	// 编辑基本信息和职业信息都复用此方法
	@RequestMapping("/editIndNewestPD")
	public void editIndNewestPD(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String custId = request.getSession().getAttribute("indCustId")
				.toString();
		PdH pdh;
		pdh = customerService.findIndNewestPDByCustId(custId);

		pdh.setModelId(request.getParameter("modelId"));
		if (request.getParameter("confirmPdvalue") != null)
			pdh.setConfirmPdvalue(Double.valueOf(request
					.getParameter("confirmPdvalue")));
		pdh.setConfirmDate(request.getParameter("confirmDate"));
		pdh.setConfirmUserId(request.getParameter("confirmUserId"));

		customerService.updateIndPdH(pdh);

		response.getWriter().print("{success:true}");
	}

	// 获取担保列表
	@RequestMapping("/listIndHistoryPD")
	public void listIndHistoryPD(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));

		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("indCustId").toString();
		List<PdH> list = new ArrayList<PdH>();

		if (custId != null && !custId.equals(""))
			list = customerService.findIndHistoryPDByCustId(custId);

		// 分页列表list
		List<PdH> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (PdH ca : pageList) {
			ModelHis model;
			model = modelService.findModelHisById(ca.getModelId());
			ca.setModelId(model.getModelName());
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		log.info("JSON: " + myCustJO);
		out.print(myCustJO);
	}

	// 添加客户信息 历史评级信息
	@RequestMapping("/addIndPD")
	public void addIndPD(HttpServletRequest request,
			HttpServletResponse response, PdH pdh, Model model)
			throws IOException {
		log.info(("custId: " + pdh.getCustId()));
		pdh.setCustId(request.getSession().getAttribute("indCustId").toString());

		customerService.insertIndPdH(pdh);

		response.getWriter().print("{success:true}");
	}

	// 删除客户历史评级信息
	@RequestMapping("/deleteIndPD")
	public void deleteIndPD(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("indCustId") != null
				&& !request.getSession().getAttribute("indCustId").toString()
						.equals(""))
			customerService.deleteIndPDById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 编辑客户 历史评级信息
	@RequestMapping("/editIndPD")
	public void editIndPD(HttpServletRequest request,
			HttpServletResponse response, PdH pdh) throws IOException {
		String addSerialNo = request.getParameter("serialNo");
		// indOguarantee.setSerialNo(addSerialNo);
		pdh.setCustId(request.getSession().getAttribute("indCustId").toString());

		customerService.updateIndPdH(pdh);

		response.getWriter().print("{success:true}");
	}

	// 获取单一评级信息
	@RequestMapping("/getIndPD")
	public void getIndPD(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		PdH pdh = new PdH();
		pdh = customerService.findIndPDById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(pdh);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
		log.info("json: " + addressJO);
	}

	/*
	 * 保存资产负债表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/savePublicBalance")
	public String savePublicBalance(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项，此为标记项，标记资产还是负债的中间点
			if (paraName.equals("type"))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(PublicBalanceSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));

			// rd.setDisplayFlag(ReportModelInit.getInstance());

			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			// log.info("disPlayNo: "+rd.getId().getDisplayNo());

			String value = request.getParameter(paraName);

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(PublicBalanceSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("PBS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";

		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态并且是年报则计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService
						.findRecentReportData(report);
				// 查找前一年的年报，如果无，则置为null
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
				PublicVerify pv = new PublicVerify(reportDataList);
				if (pv.getResultList() != null && pv.getResultList().size() > 0) {
					log.info("请检查您的报表");
				} else {
					PublicFinancialIndexCalculator fic = new PublicFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(PublicFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				}
			}

		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListAsset = customerService.findSortedPublicAsset(report);
		List<ReportData> reportDataListDebt = customerService.findSortedPublicDebt(report);
		if (reportDataListAsset != null && reportDataListDebt != null && reportDataListAsset.size() + reportDataListDebt.size() == SheetNum.PUBLIC_ASSET_NUM + SheetNum.PUBLIC_DEBT_NUM) {
			model.addAttribute("PublicAssetDataList",reportDataListAsset);
			model.addAttribute("PublicDebtDataList",reportDataListDebt);

			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_PublicBalance";
	}

	/*
	 * 保存事业单位损益表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/savePublicIncome")
	public String savePublicIncome(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(PublicIncomeSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			// log.info("disPlayNo: "+rd.getId().getDisplayNo());

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(PublicIncomeSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("PIS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService
						.findRecentReportData(report);
				// 查找前一年的年报，如果无，则置为null
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
				PublicVerify pv = new PublicVerify(reportDataList);
				if (pv.getResultList() != null && pv.getResultList().size() > 0) {
					log.info("请检查您的报表");
				} else {
					PublicFinancialIndexCalculator fic = new PublicFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(PublicFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				}

			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> reportDataListIncome = customerService.findSortedPublicIncome(report);
		if (reportDataListIncome != null && reportDataListIncome.size() == SheetNum.PUBLIC_INCOME_NUM) {
			model.addAttribute("PublicRevenueDataList",
					customerService.findSortedPublicRevenue(report));
			model.addAttribute("PublicExpendDataList",
					customerService.findSortedPublicExpend(report));
			model.addAttribute("PublicSurplusDataList",
					customerService.findSortedPublicSurplus(report));
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_PublicIncome";
	}

	/*
	 * 保存事业单位损益表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveSmallIncome")
	public String saveSmallIncome(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		Object typeObj = request.getParameter("type");
		// 通过点击资产负债表进入默认为1, type=0表示暂存，type=1表示保存
		String type = "1";
		if (typeObj != null)
			type = typeObj.toString();
		else {
			type = "0";
		}

		List<ReportData> list = new LinkedList<ReportData>();
		Enumeration paras = request.getParameterNames();
		String reportScope = "";
		String accountMonth = "";
		ReportList report = new ReportList();

		if (request.getSession().getAttribute("report") != null) {
			report = (ReportList) request.getSession().getAttribute("report");
			reportScope = report.getId().getReportScope();
			accountMonth = report.getId().getAccountMonth();
		}

		// 遍历param列表
		while (paras.hasMoreElements()) {

			String paraName = (String) paras.nextElement();

			// 忽略para为type的项
			if (paraName.equals("type") || paraName.equals(""))
				continue;

			ReportData rd = new ReportData();
			ReportDataId id = new ReportDataId();
			id.setReportNo(SmallIncomeSheet.REPORTNO);
			id.setCustId(request.getSession().getAttribute("custId").toString());
			id.setReportScope(reportScope);
			id.setAccountMonth(accountMonth);
			id.setDisplayNo(ReportModelInit.getInstance().getDisplayNoById(
					paraName));
			rd.setId(id);
			rd.setReportItemNo(paraName);
			rd.setDisplayName(ReportItemInit.getInstance().getReportItem()
					.get(paraName));
			rd.setDisplayFlag(ReportModelInit.getInstance().getDisplayFlagById(
					paraName));

			String value = request.getParameter(paraName);

			// log.info("disPlayNo: "+rd.getId().getDisplayNo());

			if (value != null && !value.toString().equals("")
					&& value.toString().length() > 0) {
				try{rd.setItemValue(Double.parseDouble(value.replaceAll(",","")));}catch(NumberFormatException e){rd.setItemValue(0.0);}
			}
			else{
				rd.setItemValue(0.0);
			}

			if (rd != null) {
				list.add(rd);
			}
		}

		// 如果type=0写入暂存，如果为1表示确定状态
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setReportNo(SmallIncomeSheet.REPORTNO);
		id.setCustId(request.getSession().getAttribute("custId").toString());
		id.setReportScope(reportScope);
		id.setAccountMonth(accountMonth);
		id.setDisplayNo("0");
		rd.setId(id);
		rd.setDisplayName("标记暂存");
		rd.setReportItemNo("SIS000");
		rd.setDisplayFlag(type);

		list.add(rd);

		// 批量插入
		if (list != null && list.size() > 0) {
			customerService.insertReportDataList(list);
			model.addAttribute("flag", 1);
		}

		// 无参数传递,表示从点击资产负债表或者损益表进入
		if (typeObj == null)
			type = "2";
		// 如果是保存，查询其他表是否都是保存，如果都是保存，讲该期报表的状态置为确定状态，否则为待定
		if (customerService.isReportReady(report)) {
			// 更新到确定状态
			customerService.updateReportStatus(report, ReportList.READY);
			// 确定状态计算财务指标,并写入reportData表
			if (!type.equals("2") && report.getReportKind().equals("01")) {
				List<ReportData> reportDataList = customerService.findRecentReportData(report);
				SmallCheck sc = new SmallCheck(reportDataList);
				List<String> resultList = sc.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功
					List<ReportData> previousReportDataList = customerService
							.findPreviousYearReportData(report);// 查找前一年的年报，如果无，则置为null
					SmallFinancialIndexCalculator fic = new SmallFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(SmallFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				} else {// 校验出现问题
					log.info("请检查您的报表");
				}

			}
		} else {
			customerService.updateReportStatus(report, ReportList.NOTREADY);
		}

		List<ReportData> incomeReportData = customerService.findSortedSmallIncome(report);
		if (incomeReportData != null && incomeReportData.size() == SheetNum.SMALL_INCOME_NUM) {
			model.addAttribute("SmallIncomeDataList", incomeReportData);
			// 标记是从reportData获取而不是从reportModel获取，同时为了显示该项数据
			model.addAttribute("flag", 1);
		} else {
			model.addAttribute("flag", 0);
		}

		return "customer/AddCustomer_SmallIncome";
	}

	// 报表检验
	@RequestMapping("formVerify")
	public String formVerify(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ReportList report = (ReportList) request.getSession().getAttribute(
				"report");
		String custId = request.getSession().getAttribute("custId").toString();
		CustInfo custInfo = customerService.findCustByCustId(custId);

		// 企业法人
		String custType = custInfo.getCustType();
		if (custType.equals("01")) {
			if ( report.getReportType().equals("02")) {// 小企业
				List<ReportData> reportDataList = customerService.findRecentReportData(report);// 查找前一年的年报，如果无，则置为null
				SmallCheck sc = new SmallCheck(reportDataList);
				List<String> resultList = sc.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功，无错误:计算财务指标数据
					log.info("报表check通过");
				} else {
					log.info("请检查您的报表");
				}

				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				if (report.getReportKind().equals("01")) {
					// 更新到确定状态
					customerService
							.updateReportStatus(report, ReportList.READY);
					// 确定状态并且是年报计算财务指标,并写入reportData表
					SmallFinancialIndexCalculator fic = new SmallFinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(SmallFinancialIndexSheet.REPORTNO,
							fic.getResultMap(), report);
				}

				model.addAttribute("verifyResultList", resultList);
				return "customer/AddCustomer_SmallVerify";
			} else {// 一般企业
				List<ReportData> reportDataList = customerService.findRecentReportData(report);// 查找前一年的年报，如果无，则置为null
				EnterCheck ec = new EnterCheck(reportDataList);
				List<String> resultList = ec.check();
				if (resultList == null || resultList.size() == 0) {// 校验成功，无错误:计算财务指标数据
					log.info("报表check通过");
				} else {
					log.info("请检查您的报表");
				}
				List<ReportData> previousReportDataList = customerService
						.findPreviousYearReportData(report);

				if ( report.getReportKind().equals("01")) {
					// 更新到确定状态
					customerService
							.updateReportStatus(report, ReportList.READY);
					// 确定状态并且是年报计算财务指标,并写入reportData表
					FinancialIndexCalculator fic = new FinancialIndexCalculator(
							reportDataList, previousReportDataList);
					fic.computeIndex();
					insertFinancialIndex(EnterFinancialIndex.REPORTNO,
							fic.getResultMap(), report);
				}

				model.addAttribute("verifyResultList", resultList);
				return "customer/AddCustomer_EnterVerify";
			}
		}
		// 事业单位
		else if (custType.equals("02")) {
			List<ReportData> reportDataList = customerService
					.findRecentReportData(report);
			// 查找前一年的年报，如果无，则置为null
			List<ReportData> previousReportDataList = customerService
					.findPreviousYearReportData(report);

			// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
			PublicVerify pv = new PublicVerify(reportDataList);
			if (pv.getResultList() != null && pv.getResultList().size() > 0) {
				log.info("请检查您的报表");
			} else {
				log.info("报表check通过");
			}

			if (report.getReportKind().equals("01")) {
				// 更新到确定状态
				customerService.updateReportStatus(report, ReportList.READY);
				
			}
			
			// 确定状态并且是年报计算财务指标,并写入reportData表
			PublicFinancialIndexCalculator fic = new PublicFinancialIndexCalculator(
					reportDataList, previousReportDataList);
			fic.computeIndex();
			insertFinancialIndex(PublicFinancialIndex.REPORTNO,
					fic.getResultMap(), report);

			model.addAttribute("verifyResultList", pv.getResultList());

			return "customer/AddCustomer_PublicVerify";
		}
		// 金融同业
		else if (custType.equals("03")) {
			List<ReportData> reportDataList = customerService
					.findRecentReportData(report);
			// 查找前一年的年报，如果无，则置为null
			List<ReportData> previousReportDataList = customerService
					.findPreviousYearReportData(report);

			// 首先验证数据，验证OK，则进行财务指标的计算，否则不进行财务指标计算
			FinanVerify pv = new FinanVerify(reportDataList);
			if (pv.getResultList() != null && pv.getResultList().size() > 0) {
				log.info("请检查您的报表");
			} else {
				log.info("报表check通过");
			}
			if (report.getReportKind().equals("01")) {
				// 更新到确定状态
				customerService.updateReportStatus(report, ReportList.READY);
				// 确定状态并且是年报计算财务指标,并写入reportData表
				FinanFinancialIndexCalculator fic = new FinanFinancialIndexCalculator(
						reportDataList, previousReportDataList);
				fic.computeIndex();
				insertFinancialIndex(FinanFinancialIndexSheet.REPORTNO,
						fic.getResultMap(), report);
			}

			model.addAttribute("verifyResultList", pv.getResultList());

			return "customer/AddCustomer_FinanVerify";
		} else {
			return "";
		}

	}

	// 获取集团信息列表
	@RequestMapping("/listGroup")
	public void listGroup(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<GroupInfo> list = new ArrayList<GroupInfo>();
		if (custId != null && !custId.equals(""))
			list = customerService.findGroupByCustId(custId);
		// 分页列表list
		List<GroupInfo> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (GroupInfo ca : pageList) {
			ca.setMemberShip(DataDictionary.getInstance().getNameByCodeAndId(
					"MemberShip", ca.getMemberShip()));
			ca.setCurrency(DataDictionary.getInstance().getNameByCodeAndId(
					"Currency", ca.getCurrency()));
			ca.setIsOurCust(DataDictionary.getInstance().getNameByCodeAndId("YesNo",ca.getIsOurCust()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 集团信息
	@RequestMapping("/addGroup")
	public void addGroup(HttpServletRequest request,
			HttpServletResponse response, GroupInfo groupInfo, Model model)
			throws IOException {
		log.info(("groupInfo: " + groupInfo.getGroupName()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			groupInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());

		customerService.insertGroup(groupInfo);

		response.getWriter().print("{success:true}");
	}

	// 删除客户地址信息
	@RequestMapping("/deleteGroup")
	public void deleteGroup(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteGroupById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 获取单一集团信息
	@RequestMapping("/getGroup")
	public void getGroup(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		GroupInfo groupInfo = new GroupInfo();
		groupInfo = customerService.findGroupById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(groupInfo);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	// 编辑客户集团信息
	@RequestMapping("/editGroup")
	public void editAddress(HttpServletRequest request,
			HttpServletResponse response, GroupInfo groupInfo)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			groupInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateGroup(groupInfo);
		}

		response.getWriter().print("{success:true}");
	}

	// 获取成员信息列表
	@RequestMapping("/listMember")
	public void listMember(HttpServletRequest request,
			HttpServletResponse response, String custId) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		// 分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		if (custId == null || custId.equals(""))
			custId = request.getSession().getAttribute("custId").toString();
		List<MemberInfo> list = new ArrayList<MemberInfo>();
		if (custId != null && !custId.equals(""))
			list = customerService.findMemberByCustId(custId);
		
		// 分页列表list
		List<MemberInfo> pageList;
		if (list.size() > start + limit)
			pageList = list.subList(start, limit + start);
		else
			pageList = list.subList(start, list.size());

		for (MemberInfo ca : pageList) {
			ca.setMemberShip(DataDictionary.getInstance().getNameByCodeAndId(
					"MemberShip", ca.getMemberShip()));
			ca.setCurrency(DataDictionary.getInstance().getNameByCodeAndId(
					"Currency", ca.getCurrency()));
			ca.setIsOurCust(DataDictionary.getInstance().getNameByCodeAndId("YesNo", ca.getIsOurCust()));
		}

		JSONArray myCustJO = JSONArray.fromObject(list);
		out.print(myCustJO);
	}

	// 添加客户信息 集团信息
	@RequestMapping("/addMember")
	public void addMember(HttpServletRequest request,
			HttpServletResponse response, MemberInfo memberInfo, Model model)
			throws IOException {
		log.info(("MemberInfo: " + memberInfo.getGroupName()));

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			memberInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());

		customerService.insertMember(memberInfo);

		response.getWriter().print("{success:true}");
	}

	// 删除客户成员信息
	@RequestMapping("/deleteMember")
	public void deleteMember(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals(""))
			customerService.deleteMemberById(addSerialNo);

		response.getWriter().print("{success:true}");
	}

	// 获取单一成员信息
	@RequestMapping("/getMember")
	public void getMember(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String addSerialNo = request.getParameter("serialNo");
		MemberInfo memberInfo = new MemberInfo();
		memberInfo = customerService.findMemberById(addSerialNo);
		JSONObject addressJO = JSONObject.fromObject(memberInfo);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(addressJO);
	}

	// 编辑客户成员信息
	@RequestMapping("/editMember")
	public void editMember(HttpServletRequest request,
			HttpServletResponse response, MemberInfo memberInfo)
			throws IOException {

		if (request.getSession().getAttribute("custId") != null
				&& !request.getSession().getAttribute("custId").toString()
						.equals("")) {
			memberInfo.setCustId(request.getSession().getAttribute("custId")
					.toString());
			customerService.updateMember(memberInfo);
		}

		response.getWriter().print("{success:true}");
	}
}
